Avoid Logs To The Console Breaking In IE7 & 8

As I’m sure you’re aware, if you try to log to the console in a browser like IE7 or 8 where there is no console like this:

Then it will break your script. Of course, the simple solution to this is to make sure there’s no call to the console object in your code, however during testing this can be quite tedious. At the expense of a few lines of code you can get around this by adding this at the top of your JS file:

Firstly we create a new object called c, and then add a method called log which first checks if the console exists, and then calls the regular console.log() method if it does.

You would use this the same as the console, except it’s a bit quicker to write:

Now if you accidentally, or purposefully, leave that in your code it won’t break IE7 or 8, or any other browser which doesn’t implement the console object. You could of course add other console methods quite easily.

Because I’m lovely, here’s an already minified version of the  c object, so it takes up even less space.

Go forth and log to the console as much as you wish.