Debugging Javascript with Console

A quick review.


//how to print a log statement
console.log('That thing you expected to happen is now happening!');

//how to break production sites in internet explorer

//how to log safer
function l(str){if(window.console&&console.log){console.log(str);}}
l('I can forget this in my code base without breaking anything!');


//how to figure out what's calling what

//console.trace example:
function a() { console.trace(); }
function b() { a(); }
function c() { b(); }
function d() { b(); }

if (Math.random() < 0.5) {
else {

If you copy and paste that console.trace example into the Chrome Console you'll get a full stack trace like this one:

Console.Trace in script

While that's incredibly useful it gets a little better for daily use. Since you'll almost always be using console.trace in the context of functions defined in a file somewhere (rather than on the spot in the console) you'll get clickable line numbers along with your stack trace output:

Console.Trace in console

Leave a Comment