Method 1: Using console.log() Method to Print to Console
The console.log() method is utilized to print any variable which is defined in it. It can be used for descriptive text. The method returns the value that the user passed as a parameter. Developers employ this method for debugging purposes. The console.log() is the widely used method to print output. The output may be calculated results or messages.
The syntax of the console.log() method is provided below:
The message parameter specifies the information to be displayed using the console.log() method.
For better understanding, the following example code is used to practice the console.log() method.
A variable “a” is initialized, and then it is printed on the console using the console.log() method.
The output shows that the value of variable “a” is printed on the console.
Method 2: Using console.error() Method to Print to Console
The console.error() method is the most common method for displaying the error message in the console window. The syntax of the console.error() message is provided below:
This method also accepts only parameters which will be displayed as an error message on the console. Let’s practice the console.error() via the following example code:
A variable is initialized, and that variable is passed to the console.error() method as an argument.
After executing a code in the browser console, a highlighted text is displayed as shown in the output’s image.
Method 3: Using console.warn() to Print to Console
In this code, the console.warn() method is utilized to print a warning message to the users.
The highlighted text in yellow is used to present the warning message to the user by using the console.warn() method.
Method 4: Using console.info() to Print to Console
Another method console.info() is adapted to print the general information to the user. To use this method, the syntax is written below:
An example is used to send a specific or user-defined message to the console window.
The console.info() is employed to print a general message/information to the user through the console.