How to Print a Message to the Error Console Using JavaScript
Read this tutorial and learn about the role of console object in JavaScript and, also, learn the way of printing the message to the error console easily.
Printing to the console is straightforward and useful. In JavaScript, the console is an object that provides access to the browser's debugging tools. It offers several methods for outputting customized debugging information, including printing messages to the error console.
The following example displays the default message:
Javascript console.log displays the default message
This example shows a red error message:
Javascript console.error shows a red error message
This outputs a warning message (browser UI typically shows an exclamation mark):
Javascript console.warn shows warning message
This example outputs an information message (browser UI typically shows an 'i' icon):
Javascript console.info outputs an information message
You can use the <kbd class="highlighted">console.error</kbd> method to print errors. Here <kbd class="highlighted">"x"</kbd> is the variable's name:
Javascript console.error method
The <kbd class="highlighted">console.error()</kbd> method outputs an error message to the Web Console. The <kbd class="highlighted">console.exception()</kbd> method is an alias for <kbd class="highlighted">console.error()</kbd> and functions identically.