How do I print a message to the error console using JavaScript?


To print a message to the error console, use the console object. Here’s an example −

The following will show a red error message −

console.error(message);

The following gives you the default message −

console.log(message);

The following gives you the warning message −

console.warn(message);

The following gives an information message −

console.info(message);

Add CSS to the log message −

console.log('%c Add message!, "background: green; color: white;");

Use the following to print error. Consider “num” as the variable name −

console.error('num=%d', num);

For complete API reference, refer Console API Reference.

Ali
Ali

Updated on: 16-Jun-2020

183 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements