Skip to content
jfraboni edited this page Nov 26, 2014 · 7 revisions

Logging in JavaScript and Node.js with console.log()

We’ll use node’s console object a lot to print output to the command-line, it looks like this:

#!/usr/bin/env node

console.log("Welcome to our awesome node app!"); // prints: Welcome to our awesome node app!

If you ran this in the node REPL or in an app you’ll see printed to the command line, “Welcome to our awesome node app!”. console.log() is also available for JavaScript run in a browser, and when invoked, will print textual statements to the browser's debug-console.

When we use console.log(), we are actually executing a function called log on the console object. Whenever you see a function name followed by parenthesis, like, someFunctionName(), we are actually invoking or calling that function. The log function takes an Object and will print some evaluated version of that Object. In other words, what you log doesn't always have to be a String (text), it could be any type of Object. The log function will try to run a toString() function on that Object to generate a printable version of that Object.

Printing stuff allows us not only to give feedback to ourselves or users, but also comes in handy as a debugging tool while writing an app. This process of printing values for debugging is called introspection.

Code a little

Take the quiz

© 2014-2015 Operation Spark