Console Error Prevention

What is the best approach to prevent errors when console.log calls remain in JavaScript and it runs in browsers without a console or with the console turned off. Is there a way that it can be automatically redefined to become a javascript alert, for example?

+5
source share
3 answers
if(!window.console) console = {log: function(s) {alert(s);}};

You can, of course, add more features that the console usually has.

+4
source

, console, typeof, window.console, , , , ReferenceError.

:

if (typeof console == "undefined") {
  window.console = {
    log: function () {
      // do nothing
    }
  };
  console.warn = console.debug = console.log;
} 
+3

Here is what I use: -

if(typeof(console) != "undefined")
0
source

All Articles