DEV Community

Cover image for JavaScript Console Types and Tricks
Akash K
Akash K

Posted on

JavaScript Console Types and Tricks

Hello Everyone!

As a web developer, we always use "console" to debug our code. In this post, we'll see some types of console that will help us in better debugging.

1) Console.log

As a web developer we are so familiar with this console type.

console.log() is used to output a message or content to the web console. The message can be of any type.
console.log example

2) Console.warn

console.warn() outputs a warning message to the web console.
This is more similar to console.log but the warning message will have a small exclamation point next to it.
console.warn example

3) Console.error

console.error() outputs a error message to the web console.
This is more similar to the console.warn but the error message will be in red color and have a error symbol next to it.

console.error example

4) Console.trace

console.trace() method outputs a stack trace to the web console.
This shows the trace of the file in which this particular console got called. Even you can give a trace label to identify a particular trace.
console.trace(label)

Still confused?? Hopefully the below image will give some insight.

console.trace example

This outputs,

output

5) Console.clear

console.clear() clears the console if the console allows it.
console running on the browser will allow to clear it whereas a console displaying on the terminal like the one in Node will not allow it.

6) Console.count

console.count() logs the number of times that this particular call to count() has been called.

Similar to trace, you can give a label to identify the count.
console.count(label)

console.count example

The output will be like

output

7) Console.group

console.group() method creates a new inline group in the web console log. Any subsequent console.log() will be indented by an additional level, making it look like a group until the console.groupEnd() is called.

This console.group() method should be used with the console.groupEnd() method. These methods are used to group the console logs.

console.group example

8) Console.time

console.time() method starts a timer to track how long an operation takes. You can give each timer a unique name, and may have up to 10,000 timers running on a given page.

This method should be used with the console.timeEnd() method which is used to stop the timer.

Using these methods you can track the execution time of a particular code block or function.

console.time example
This outputs the run time of function foo,

output

NOTE: The label in the time() method should match with the timeEnd() method to stop the tracker. Since you can run multiple trackers at the same time, label is used to stop a particular tracker.

9) Console.timeLog

console.timeLog() method logs the current value of a particular timer that was previously started by console.time().
Using this method you can check the value of that particular timer in between the execution.

console.timeLog example
This outputs
output
NOTE: The label in the time() method should match with the timeLog()

Thank you so much if you have read it so far. This is my first blog post. Please do share your thoughts on this.

If you liked reading this blog, please don't forget to like ❤️, and share 🤩 in order to show your support as your support means a lot to me.
method.

Top comments (7)

Collapse
 
tracygjg profile image
Tracy Gilmore

Hi Akash,
An excellent article covering some of the console API. One method I use quite often is console.table. See what you think.
For readers wanting a complete technical definition of the console API I suggest they visit MDN.
Best regards.

Collapse
 
akashkumaravel profile image
Akash K

Hi Gilmore,
Thanks for sharing this. This is very new to me!! :)

Collapse
 
not-ethan profile image
Ethan

AWESOME! People don't take full advantage of all the methods that you can use on the console object. One of by favorites are console.table for objects

Collapse
 
pulkitsingh profile image
Pulkit Singh

Yes, you can use console.log({object}) to log an object to the console in JavaScript. The curly braces {} around the object will display the object in a more readable format, making it easier to understand the object's properties and values. This can be especially useful when logging multiple objects from different parts of your code, as it can help you keep track of which object is being logged.

Collapse
 
akashkumaravel profile image
Akash K

Thanks for sharing!... @pulkitsingh

Collapse
 
fruntend profile image
fruntend

Сongratulations 🥳! Your article hit the top posts for the week - dev.to/fruntend/top-10-posts-for-f...
Keep it up 👍

Collapse
 
reacthunter0324 profile image
React Hunter

good console explains!
thank you