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?
Node. js provides a console module which provides tons of very useful ways to interact with the command line. It is basically the same as the console object you find in the browser. The most basic and most used method is console. log() , which prints the string you pass to it to the console.
The console method log() displays the toString representation of any object passed to it. The Console method dir() displays an interactive list of the properties of the specified JavaScript object.
As said above, the console. log is asynchronous and non-blocking, so it would not slow your application too much except one tick for the function invocation. But it is a good habit to use some module to turn some logs of certain level off when deploy it in production instead of using console.
It's a function call. It goes to the call stack.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With