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

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.


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

1. Defination

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

Tags:

Node.Js