Window.console in JavaScript

In JavaScript, window.console or console object is under-rated with only console.log() being used most of the time. However, the console object comes with a lot of advanced features.

In this article, we will be discussing the functions available on the console that can be handy at times.

A deeper look into the console

Besides console.log(), there are a lot of other methods also available. You can try looking for the suggestions list in the browser’s console.

console.log()

console.log() is the most commonly used function in JavaScript to log values. It just logs/prints the value that is passed to the function.

We can also concatenate multiple values using console.log(). Simply call console.log() with multiple parameters and all the values will be concatenated.

You can even pass multiple objects and the browser’s console will show them side by side.

Following are some of the examples of concatenation with console.log:

console.table()

While console.log() can be used to print an object, we can also use console.table() to pretty print an object in the form of a table.

Just pass the object to print the object in a more readable format.

This is image title

Notice, how it prints the values for the nested object.

console.warn()

It is used to print warning messages in the browser’s console.

console.error()

It is used to print error messages in the console.

console.time()

This can be used to calculate the time elapsed by our code to execute.

Before starting the execution of any operation, start the timer with console.time(‘TIMER_NAME’) and after the execution of the code, end the timer with console.timeEnd(‘TIMER_NAME’).

As soon as you call the timeEnd function, the elapsed time will get printed in the console.

The timer name should be same for time() and timeEnd()

console.clear()

This command is used to clear the console.

Running the last command resulted in the console being cleared with the following message.

Thank you for reading !

#JavaScript #Front End #Programming #Web Development #Development

Window.console in JavaScript
1 Likes18.80 GEEK