The most common way to print information to the console is by using the console.log() method. This will print the message to the browser’s console.
Basic Example
console.log('Hello world!');
The above example will print “Hello world!” in the browser’s console.
Printing Variables
var name = 'James'; console.log(name);
The above example will print “James” in the browser’s console.
Printing Multiple Values
var name = 'James';
var age = '20 years';
console.log('Name:', name, 'Age:', age);
The above example will print the name and age to the console.
Printing Errors
console.error('This is an error message');
The above example will print the error. It is pretty useful as most browsers highlight the errors in red color.
The console object is a very valuable Javascript tool as it can be used in almost all the web applications to debug or to inspect.