New things I learned today part 1 – console.log() is a bad idea…

For me, one of the most important aspects of my career as a software developer is learning. I love to learn new stuff, and I don’t just mean play with shiny new technologies or gadgets; I mean improve my knowledge about the tools I need to use in my work. In fact, I like to learn something new every day!

So I thought I would start writing these quick snippets to record the new things I learn each day (assuming that I do in fact learn something new each day!).

Today I learned that debugging JavaScript using console.log() is a bad idea!

Because not all browsers (ahem IE…) support console.log() when the debugger isn’t running, you may get a nullref exception!

It’s much better to use debugger; – so when you have the debugger open your code will automatically break at that point!

If you do want/need to use console.log() in your JavaScript, you should do something like this to protect against that error.

Tagged with: ,