Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between console.log() and console.error() in js?

Tags:

node.js

Looking at some videos about promises in node.js the guy uses console.error() to print things he know would error out and console.log() for everything else. Is there a main difference between these 2 functions besides printing things in distinct colours?

like image 934
Diego Ramírez-Milano Avatar asked Jul 20 '18 19:07

Diego Ramírez-Milano


People also ask

What is difference between console log and console error?

console. error is used to output error messages while console. log is the most commonly used console method and is used to log out all sorts of objects or messages. In case of some error browser automatically output the relevant error message while you have to manually log out objects using console.

What is console error JavaScript?

The console. error() method outputs an error message to the Web console. Note: This feature is available in Web Workers.

What is the difference between console log and return in JavaScript?

The main purpose of the return value, is to use the function return value like you would have use a variable value. the console. log(); is a function (you can see the brackets) that will write the argument value on the console. And only the debuging console, in a browser you will not see anything on the screen.

What is the difference between console log and print?

print will just print the text to console. console. log() actually records it and we can use it for many purposes like email it for bug report.


1 Answers

console.error() writes to stderr, whereas console.log() writes to stdout as described in the doc.

In a default run of nodejs, both stdout and stderr go to the console, but obviously, they could be directed different places and could be used differently. For example, when using tools such as forever, the two streams are logged to separate log files so they can be examined separately.

The presumption is that console.error() may contain more serious information that might want to be looked at separately, but that is really up to how you use console.log() vs. console.error() in your own program.

like image 75
jfriend00 Avatar answered Sep 21 '22 02:09

jfriend00