JavaScript: Explore Different Types Of Console Methods.

Are you only familiar with console.log() and want to know more about console methods in detail? This article drives you through every console method available in JavaScript.

Nitin Gutte
TheLeanProgrammer

--

As a beginner in JavaScript, you used the console.log() method a lot of times to display the result of your code. JavaScript introduced different types of methods to find errors in the code.

This article gives you a basic understanding of console methods.

What is the Console?

The console is the tool where you can test or check your code is working properly or not.

It can be used to access the browser to troubleshoot the errors related to your code.

Remember a console is an object that can access the methods like log() using the dot (.) operator.

Syntax :

Object.Method(parameters);

Different types of methods are as follows.

1. console.log() :

As a newbie programmer, you learned to display the output of the program from the console.log() function or method.

As we know this method can display messages or mathematical expressions in the console.

Syntax :

console.log(parameter);

Example :

Output :

2. console.info() :

The console.info() is a method in JavaScript that is used to display the important messages to the console.

Syntax :

console.info(parameter);

In this method, the parameter can be of string, objects, and arrays.

Example :

Output :

3. console.clear() :

As simple as the name given, it is used to clear the mess in the console.

Syntax :

console.clear();

This method doesn’t require any parameters.

Example :

Output :

By default, the console logs the message ‘Console was cleared’.

4. console.error() :

This method is used to find out errors in the code. Console.error() method is developed for debugging.

Syntax :

console.error(parameter);

Parameters of string type are required.

Example :

Output :

The output of the console is in red color.

5. console.warn() :

If something goes wrong then this method is used to display warning messages to the console.

Syntax :

console.warn(parameter);

This method also requires a string type parameter.

Example :

Output :

By default, the color of the message is in yellow color.

5. console.assert() :

The assert method only displays the message only if the given expression is false.

Syntax :

console.assert(parameters(Expression, Message);

We can only pass two parameters in the method :

Expression: This is the boolean expression that is true or false can be passed.

Message: This is a string message that displays if the expression is false. You can also pass objects and arrays.

Example :

Output :

In this example the ID is not found then the console logs the following message ‘There is not a valid ID found’.

6. console.count() :

As we used the count variable to count the number of repetitions in the loop.

The console.count() method also counts the number of times it is called in the program.

Syntax :

console.count(label);

Parameters which is passed in the method are a type of label which is a string.

If the parameters are not present in the method then the default parameters will be added.

Example :

Output :

As the loop goes through every iteration the console adds the ‘default’ name with a number.

7. console.trace() :

The console.trace() method tracks the execution of the code from starting point to the endpoint and how the code ended at a certain point.

Syntax :

console.trace(label);

This method accepts any data as a single parameter.

Example :

Output :

8. console.table() :

This method displays data in the form of tables.

The first compulsory parameters passed are in the form of Arrays or Objects, and it requires another parameter as a column(optional parameter).

Syntax :

console.table(any data);

or

console.table(any data, data columns);

Example :

Output :

9. console.time() and console.timeEnd() Methods :

The time method is used to track how long any type of operation required to complete the task.

It starts from the console.time() method and ends to console.timeEnd() method. This is the period where this method calculates the total time.

Syntax :

console.time(label);

. . . .

console.timeEnd(lable);

Example :

Output :

The console logs the time with the message passed to it.

10 Grouping Methods :

1. console.group() :

This method allows grouping the content logs of the code. It is used to start the group.

Syntax :

console.group(label);

The parameter label which is the type of string is optional in the method.

2. console.groupEnd() :

This method is used to end the group.

Syntax:

console.groupEnd();

No parameters used in this method.

3. console.groupCollapsed() :

This method is used to structure the separate group in a collapsed manner. It's like nesting in the loop.

Syntax :

console.groupCollapsed(label);

This method uses only one label parameter as a string type.

Example :

Output :

Conclusion :

This article introduced you to the different types of console methods. You can use these methods in your debugging journey.

Some methods look simple, but they are very useful in finding the bugs in your code.

If something goes wrong in your code these methods give you the ability to find the solution to your problem.

Photo by Igor Karimov on Unsplash

If you enjoyed this article please comment, share and follow me.

Thank You For Reading!

Don’t forget to follow The Lean Programmer Publication for more such articles, and subscribe to our newsletter tinyletter.com/TheLeanProgrammer

--

--

Nitin Gutte
TheLeanProgrammer

Cybersecurity and Blockchain Enthusiast. Learning to code by teaching others in a simple way. Completed my Diploma in Information Technology.