Thursday, April 9, 2015

Console Tab in Chrome Developer Tools

Console Tab

In a previous blog post, I talked about Elements tab in Chrome developer tools and explained how they are useful in debugging. Today I am going to talk about some features of the Console tab which are again very helpful for debugging web applications.


Gone are the days, when people used to put alert() statements everywhere throughout the code in order to check variable values or control flow in the JavaScript code. Now we can use console.log() statements in order to see what's going on in the client side code. We can output all the values and the results are visible in the Console tab.


If we want to output the message as a warning, we can use console.warn() statement. These messages appear with a yellow warning sign. These are useful if you want to put some messages for less likely scenarios. We can even filter all the warnings together.


Messages output using statement come with a blue info sign. These can also be filtered.


Using console.error() statement we can output the error in the console tab. It appears with a red color. We can also navigate to where the error occurred directly as the source is displayed right next to the error. 


console.assert can be used for testing. For example,

console.assert(10 == 5*4,"Items Not Equal");

So it will break at this point and take us to the script where the test failed.


Using a lot of console.log statements can become unwieldy. So in order to manage log statements better, we can group them together."Group Example");

        console.log("Message 1");

        console.log("Message 2");

So here the 2 log messages will come under a single group. Similarly, we can also use console.groupCollapsed() if we want the group to appear as collapsed.


In order to find the time taken by a snippet of code, we used to get the current time before and after and subtract the two timestamps. Using console.time() makes it more convenient and quicker to find out the time.

        console.time("Loop Time");

        for (var i = 0; i < 100000; i++) {}

        console.timeEnd("Loop Time"); 

console.time() denotes the starting point and console.timeEnd() denotes the end point.


console.dir() prints the JSON representation of the supplied object. For example,


will print the whole document's JSON representation.


console.dirxml() prints the XML representation of the supplied object.


will print the whole document's XML representation.


Using console.count we can keep the count without maintaining a counter explicitly. Another very handy feature for debugging.


Using console.profile() we can start and using console.profileEnd() end profile. Profile is pretty useful to find out what all code got executed and how much time each part took. Its similar to SQL Server profiler.


In order to see the stack trace I can put this statement anywhere in my JavaScript code. This will show me the stack trace of who called this statement.

Execute JavaScript

Other than these useful commands, we can also execute any JavaScript code in the console tab directly. I can inspect or change the values of any variables or elements.


In this post, we discussed about different console tab commands and how they can be helpful in debugging and development. Once you start using it, you will realize how useful it is in everyday development and debugging.
For future updates to my weekly blog, please subscribe to the blog and follow me on Twitter. Until then Happy Coding :)

1 comment: