How to Print a Message to the Error Console Using JavaScript

Printing in the console is easy and at the same time an interesting thing. The console in JavaScript is an object providing access to the browser debugging console. It has many methods that help to print customized debugging. You can use the console object to output a message to the error console.

The following example displays the default message:

Javascript console.log displays the default message
let message = "Welcome to W3Docs"; console.log(message);

This example shows a red error message:

Javascript console.error shows a red error message
num1 = 30; num2 = 80; if (num1 > num2) { console.error('%d is greater then %d', num1, num2); } else { console.error('%d is less or equal to %d', num1, num2); } // Press F12 on your keyboard to view the message in the console view.

This gives a warning message with the exclamation mark in front of it:

Javascript console.warn shows warning message
let message = ["It's a warning message"]; console.warn(message); // Press F12 on your keyboard to view the message in the console view.

This example outputs an information message with an 'i' letter in front of the message:

Javascript console.info outputs an information message
let message = ["It's a information message"]; console.info(message); // Press F12 on your keyboard to view the message in the console view.

You can use the console.error method to print error. Here "x" is the variable's name:

Javascript console.error method
let x = 20; console.error('x=%d', x);

The console.error outputs an error message to the Web Console. The console.exception() method is the alias for console.error(). Functionally, they do the same thing.