Open In App

How to Print a Message to the Error Console Using JavaScript ?

Last Updated : 18 Oct, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

This article will show you how to print a message using JavaScript on the error console. There are three methods to display the error console, these are:

Approach 1: Using console.error() Method

The console.error() method is used to display an error message on the console. This method is used for testing purposes. The error message is sent as a parameter to the console.error() method.

Syntax:

console.error( message );

Example:

JavaScript
const x = 20;

if (x != 30) { 
  console.error('%d is not equal to 30', x); 
} 
else { 
  console.error('%d is equal to 30', x); 
}

Output:

20 is not equal to 30

Approach 2: Using console.warn() Method

The console.warn() method is used to write a warning message in the console. To check the warning message, open the console panel to display the output (warning message).

Syntax:

console.warn( message )

Example:

JavaScript
let str = ["Welcome to GeeksforGeeks"]; 
console.warn(str);

Output:

Welcome to GeeksforGeeks

Approach 3: Using console.info() Method

The console.info() method is used to write a message in the console. It indicates an important message about any element or object. The message is sent as a parameter to the console.info() method.

Syntax:

console.info( message )

Example:

JavaScript
let str = ["Welcome to GeeksforGeeks"]; 
console.info(str);

Output
[ 'Welcome to GeeksforGeeks' ]

Next Article

Similar Reads

three90RightbarBannerImg
  翻译: