64

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 two functions besides printing things in distinct colours?

Mark Rotteveel
110k241 gold badges160 silver badges233 bronze badges
asked Jul 20, 2018 at 19:13
0

3 Answers 3

81

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.

answered Jul 20, 2018 at 19:17
Sign up to request clarification or add additional context in comments.

1 Comment

the real convention is that stdout must contain data, and only data that the program is supposed to produce in terms of its normal usage. everything else (errors but also info / debug messages) goes to stderr. for example, ls only ever writes filenames to stdout (in format specified by its arguments), cp will never write anything to stdout (it's not intended to produce any data). both can write to stderr if there is reason for it.
9

Here are some detailed descriptions of the difference between console.log() and console.error() on the bases of different aspects.

1. Definition

Console.log()

Added in: v0.1.100.
Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

Console.error()

Added in: v0.1.100 .
Prints to stderr with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

2. Syntax

Console.log()

console.log(obj1 [, obj2, ..., objN]);
console.log(msg [, subst1, ..., substN]); 

Console.error()

console.error(obj1 [, obj2, ..., objN]);
console.error(msg [, subst1, ..., substN]);
console.exception(obj1 [, obj2, ..., objN]);
console.exception(msg [, subst1, ..., substN]); 

3. Parameters

Console.log()

(i) A list of JavaScript objects to output. The string representations of each of these objects are appended together in the order listed and output.

obj1 ... objN

(ii) A JavaScript string containing zero or more substitution strings.

msg

(iii) JavaScript objects with which to replace substitution strings within msg. This gives you additional control over the format of the output.

subst1 ... substN

Console.error()

Note: console.exception() is an alias for console.error(); they are functionally identical.

(i) A list of JavaScript objects to output. The string representations of each of these objects are appended together in the order listed and output.

obj1 ... objN

(ii) A JavaScript string containing zero or more substitution strings.

msg

(iii) JavaScript objects with which to replace substitution strings within msg. This gives you additional control over the format of the output.

subst1 ... substN

4. Browser compatibility

Console.log() and Console.error() Both have almost same compatibility with all the popular browsers.
Note:

(i) In case of google chrome Substitution strings in version 28, if a negative value is passed to %d, it will be rounded down to the closest negative integer, so -0.1 becomes -1.

(ii) Substitution strings Both With internet explorer(10) %c is not supported, %d will render as 0 when it is not a number

CSSBurner
2,17520 silver badges19 bronze badges
answered Jul 21, 2018 at 6:30

Comments

-3

Stdout is buffered while stderr isn't.

answered Jul 20, 2018 at 19:36

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.