views:

88

answers:

3

In node.js you can use console.log or sys.puts to print out to the screen.

What is the preferred method and what is the difference between these?

+5  A: 

sys.puts simply prints the given string in the logs.

But if you want to print a more complex object (Array, JSON, JSObject) you have to use console.log because you want to "look inside" of the object.

sys.puts would give you only "[object object]" for example.

Elias
yes, however you don't HAVE to use console logs to see the object. sys.puts("check out this funky object in detail: " + sys.inspect(yourobject));
dryprogrammers
A: 

Both just write to the stdout stream. The difference is that sys.puts just toString's the first argument, and console.log takes multiple arguments, and will sys.inspect the first arg if it's not a string.

isaacs
+1  A: 

Also you can use console.log without requiring the sys module.

daralthus