Simple JavaScript debugging with Chrome’s console

All to often of late, I have seen people having trouble trying to debug some JavaScript or other.

Some were stumped by the white screen of death which confronted them the minute their script failed silently, whilst others were trying to debug their program using an alert dialogue box (a truly tedious process).

Well, good news it at hand. There is another, more efficient way to do things – with the console. The console is part of  the development tools which are (to some extent) integrated into all of the modern browsers.

In this short tutorial I am going to focus on using Chrome’s Dev Tools, but there’s no reason you couldn’t follow along using Safari’s WebKit Inspector, Firebug for FirefoxInternet Explorer’s Developer Tools or Opera’s Dragonfly.

So, assuming you’re in Chrome, open up a web page and hit the F12 key. You should see a panel open up at the bottom of your screen, like so (click to enlarge):

When you first hit F12 in Chrome

Then, just click on the Console tab on the far right.

So, I’m in the console, what now?

Let’s start off by outputting something. You can do this with the command: console.log()

Here’s an example. Copy the following code to your PC and save it as an HTML file.

When you open the page in your browser (ensuring that the console is also open), you should see something similar to this (click to enlarge):

"Hello, World!" from the console.

Neat, huh?

A real world example

Now let’s look at how we might use this to solve a real world problem. Imagine we have the following HTML and JavaScript:

At first glance everything looks great. We run our file, eagerly expecting the result of the calculation to be written to the screen, and see … nothing!

So what went wrong?

Well, a quick look at the console shows us that we have a problem on line twelve of our script (click to enlarge):

The console points to a bug

To be precise, it seems that document.getElementById("res") is returning null, which is of course correct, as the id of the span which we are trying to target is result, not res.

So let’s correct that and refresh the page.

Hmm, another error. Now we’re seeing this (click to enlarge):

Too much jQuery

This error message is a bit more cryptic, but it points us to the line where the error occurred. Something seems to be going on with the innerHTML method.

A quick look at the documentation tells us that the correct syntax is HTMLElementObject.innerHTML=text.

Oops, too much jQuery!

Once we have corrected this error (removing the brackets and replacing them with an equals sign) and refreshed the page, everything works as expected.

So what’s wrong with using alert() to debug stuff?

Well, in a nutshell alert() needs to be dismissed before JavaScript execution can resume. Consider the following where we want to sum all of the elements of an array:

arr = [1,2,3,4,5,6,7,8,9,0]
t = 0;
for(i= 0; i<arr.length; i++){
 alert(t);
 t += arr[i];
}

In this example, alert() will fire ten times and will need to be clicked away ten times.

By the tenth time, you will also have forgotten what the first value actually was (although admittedly not in this simple example).

If you replace the alert(t)with console.log(t), then you will save yourself ten clicks and you will have a nice overview of the output.

Some more reasons to prefer console.log() are:

  •  It formats your objects nicely and allows you to traverse them.
  • Logging statements often have an interactive pointers to the code which issued them.
  • It allows you to deeply examine objects, rather then just seeing the results of calling toString()on them.
  • If forgotten, the end user will not get to see any logging statements.

In defence of alert()

I added this section on 20.02.2013, after Stephen Chapman pointed out a small saving grace of using alert().

This is namely, that Chrome, Safari and Firefox will display a checkbox on the second and subsequent times the alert appears, whereas Opera will display an extra checkbox which allows you to turn off JavaScript on the page.

This means that if you figure out the problem on the first or second alert you can skip the rest.

In conclusion

We’ve only just scratched the surface of using the console to debug code. It is a very powerful too and can do so much more that I have demonstrated here.

I urge you to take the time and read one of the Nettuts+ tutorials I link to in the references, you will amaze yourself at how much more productive you can become with comparatively little effort.

References:

I'm on Twitter:

Categories