Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Internet Explorer: "console is not defined" Error

Tags:

I was using console.log() in some JavaScript I wrote and an error of: console is not defined was thrown in Internet Explorer (worked fine in other browsers).

I have replaced it with:

if (console) console.log("...");

If console is undefined, I would expect the condition to evaluate as false. Ergo, the statement console.log wouldn't be executed and shouldn't throw an error.

Instead, an error of: console is not defined at character 4 is thrown.

Is this a IE bug? Or is that "if" condition really illegal? It seems absurd because if if (console) is illegal, then if (console==undefined) should be illegal too.

How are you supposed to check for undefined variables?

like image 885
matteo Avatar asked Mar 15 '12 17:03

matteo


People also ask

What does console is not defined mean?

in internet explorer the console object is not actually defined unless your developer tools are open at the time the window loads. to fix your problem, wrap all your console prints in an if statement: if (typeof window. console !== 'undefined') { ... }

What does undefined mean in console log?

The undefined is the return value of console.log() . This is standard behavior of Chrome's JS Console. Follow this answer to receive notifications.

What is console in JS?

In JavaScript, the console is an object which provides access to the browser debugging console. We can open a console in web browser by using: Ctrl + Shift + I for windows and Command + Option + K for Mac. The console object provides us with several different methods, like : log() error()


2 Answers

Other answers gave you the root cause. However, there's a better solution than using if before any call to console.*

Add this (once) before including any of your scripts that use console:

//Ensures there will be no 'console is undefined' errors window.console = window.console || (function(){     var c = {}; c.log = c.warn = c.debug = c.info = c.error = c.time = c.dir = c.profile = c.clear = c.exception = c.trace = c.assert = function(s){};     return c; })(); 

This will create a 'pseudo' console only if it doesn't exist, so that 'console is undefined' errors will go away and you won't have to ask if console exists everytime. With this, you just call console.log or any console method anywhere, without problems.

Hope this helps. Cheers

like image 187
Edgar Villegas Alvarado Avatar answered Sep 18 '22 12:09

Edgar Villegas Alvarado


If console itself doesn't exist at all, it throws an error because you're accessing an undefined variable. Just like if(abc) {} throws an error.

Since console resides in window, and window does always exist, this should work:

if(window.console) ... 

Basically, accessing an property that doesn't exist is free and doesn't throw an error (it just evaluates to undefined, failing the if condition). However, it is illegal to access an undeclared variable.

like image 32
pimvdb Avatar answered Sep 17 '22 12:09

pimvdb