Enable Console Print in ClojureScript

I’ve been playing around with ClojureScript lately and stumbled upon this little tidbit mentioned by Thomas Boyt on Twitter.

(enable-console-print!)

I’m pretty new to ClojureScript and functional programming in general, but since ClojureScript is open source, we can just look up the source code for enable-console-print! and see that it simply sets *print-fn* to console.log, which makes logging to a browser’s console way easier.

Internally *print-fn* is used by string-print, which itself is used by many other functions that print to *out*.

For example, after enabling console printing, the following functions log to the browser’s console instead of throwing an uncaught error.

(time (sort (reverse (range 42))))
(print (str 42 \newline))
(println 42)
(newline)

It’s not only more convenient to print or println to the browser’s console, it also allows other functions, like the time function, to log useful information to it.


Next: Delegates in Objective-C
Previous: Notification Center