Preventing console.log Undefined Errors

I don’t think it’s a good idea to leave


statements in production code, it’s a good idea to prevent your site from breaking if you accidentally do. I include this condition in the first script that loads on the page of most of my projects

if (typeof console == "undefined") {
    window.console = {
        log: function () {}

This JavaScript simply defines an empty function if the console is not available in the browser so stray log statements don’t break production for your valuable customers.

Published by


I am a Geek! I hope you enjoyed this recent post.

One thought on “Preventing console.log Undefined Errors”

  1. Hi Mike,

    Thanks for this snippet. I agree with your advice to avoid using console.log on production environments, however I have found this code of use when working on long-term development projects.

Comments are closed.