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.

Posted in JavaScript, Learn Something New Tagged with: ,

About Maff

Maff Rigby

I'm a certified .Net, Umbraco and AngularJS freelance developer with over 15 years experience in the IT industry. As well as writing code I love to teach; I run a number of workshops and 1-1 coaching sessions on Angular JS and Umbraco, and share what I know and learn here!

I’m social (ish)

Connect with me on LinkedIn, follow me on Twitter, or fail to find me on Facebook.