• Node.js Video Tutorials

NodeJS - console.error() Method



The Node.js console.error() method will display the error messages on the console. It prints the error lines on stderr with a new line. This method is used to create an instance of an Error class which is a base class for custom errors.

The error object contains properties such as name, message and stack trace, which can be accessed using dot(.) or bracket() notation depending on the environment used.

Let's assume, we want to print some error message on the console. Then this console.error() has a role to play. To get a better understanding, let's dive into the syntax and usage of the console.error() function of node.js.

Syntax

Following is the syntax of the Node.js console.error() method

console.error([data][, …args])

Parameters

This method accepts multiple parameters. The same are described below.

  • data − The value in this parameter is used as a primary message.

  • args − The values in this parameter are used as substitution values (the arguments are all passed to util.format()), which can be used in the primary message.

Return value

This method prints an error message to stderr with a newline.

Example

The Node.js console.error() method of node.js accepts multiple parameters.

In the example below, we are checking two variables; if both are equal we print a normal message; else we print the error message.

var x = 5;
var y = 7;
if (x === y){
   console.log("Both variables are equal")
   }
   else{
      console.error("Both variables are not equal")
   }

Output

As we can see in the output, we have accessed the substitution values by using %s (for string values) and printed the error message on the console.

Both variables are not equal

Example

In the example below, We are checking two variables; if both are equal, we print a normal message; or else we print the error message. Also, we are using other parameters which are additional substitution values to the primary message.

var x = 5;
var y = 7;
if (x === y){
   console.log("The variables %d and %d are equal", x, y)
   }
   else{
      console.log("The variables %d and %d are not equal", x, y)
   }

Output

As we can see in the output, we have accessed the substitution values by using %d (for integers) and printed the error message on the console according to the output of the condition.

The variables 5 and 7 are not equal

Example

In the example below, we are using the args parameters which are additional substitution values to the primary message.

console.error("%s: Simply Easy %s at your %s", "Tutorialspoint", "Learning",
 "fingertips");
console.error("Sachin score hundred %d", 100);
console.error("Hi %s Namaste %d %d %d", "hello", 1, 2, 3);

Output

As we can see in the output, we have accessed multiple substitution values by using %s (for string values) and %d (for integers) and printed the error message on the console.

Tutorialspoint: Simply Easy Learning at your fingertips
Sachin score hundred 100
Hi hello Namaste 1 2 3
nodejs_console_module.htm
Advertisements