- Node.js Tutorial
- Node.js - Home
- Node.js - Introduction
- Node.js - Environment Setup
- Node.js - First Application
- Node.js - REPL Terminal
- Node.js - Command Line Options
- Node.js - Package Manager (NPM)
- Node.js - Callbacks Concept
- Node.js - Upload Files
- Node.js - Send an Email
- Node.js - Events
- Node.js - Event Loop
- Node.js - Event Emitter
- Node.js - Debugger
- Node.js - Global Objects
- Node.js - Console
- Node.js - Process
- Node.js - Scaling Application
- Node.js - Packaging
- Node.js - Express Framework
- Node.js - RESTFul API
- Node.js - Buffers
- Node.js - Streams
- Node.js - File System
- Node.js MySQL
- Node.js - MySQL Get Started
- Node.js - MySQL Create Database
- Node.js - MySQL Create Table
- Node.js - MySQL Insert Into
- Node.js - MySQL Select From
- Node.js - MySQL Where
- Node.js - MySQL Order By
- Node.js - MySQL Delete
- Node.js - MySQL Update
- Node.js - MySQL Join
- Node.js MongoDB
- Node.js - MongoDB Get Started
- Node.js - MongoDB Create Database
- Node.js - MongoDB Create Collection
- Node.js - MongoDB Insert
- Node.js - MongoDB Find
- Node.js - MongoDB Query
- Node.js - MongoDB Sort
- Node.js - MongoDB Delete
- Node.js - MongoDB Update
- Node.js - MongoDB Limit
- Node.js - MongoDB Join
- Node.js Modules
- Node.js - Modules
- Node.js - Built-in Modules
- Node.js - Utility Modules
- Node.js - Web Module
- Node.js Useful Resources
- Node.js - Quick Guide
- Node.js - Useful Resources
- Node.js - Dicussion
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
To Continue Learning Please Login
Login with Google