JavaScript console.log and Internet Explorer

When coding in JavaScript, console.log is what I’m using for debug messages. Much better than using alert as it’s less embarrassing when you’re in an extreme rush and forget to remove before deploying. This week I discovered an issue with the way Internet Explorer 9 supports console.log.

It works in IE9, but only if you’ve displayed the Developer Tools. Until then, functions that call console.log will not execute anything after it is called. Since I rarely use IE without Developer Tools open, I was stumped for a while when people testing my work told me things didn’t work for them in IE when they worked perfectly in IE on my machine.

I’ve found a couple of simple ways of working around this issue. One is to test for window.console and the other is to wrap the console.log call in a try/catch. So if you’re going to use console.log, it should be like:

or

If you find that’s too long, you can always create a function. For example:

To see everything in action, view the page that demonstrates the issue and the workarounds.