Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between console.log and sys.puts in node.js?

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?

like image 704
never_had_a_name Avatar asked Oct 11 '10 07:10

never_had_a_name


People also ask

What is console log in node js?

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.

What is the difference between console log and console dir?

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.

Does console log slow down Nodejs?

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.

Is console log () added to execution stack?

It's a function call. It goes to the call stack.


2 Answers

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.

like image 200
Elias Avatar answered Oct 12 '22 20:10

Elias


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.

like image 23
isaacs Avatar answered Oct 12 '22 20:10

isaacs