Question

In Firefox/Chrome/others, typeof console.log reports "function". In IE9, (assuming the Developer Console is open, thus defining the window.console property), if you show the variable console.log in the developer console, shows

function(...) {
[native code]
}

yet it reports typeof console.log as 'object'. The standard says that functions are supposed to be reported as 'function'. Anybody know why this occurs?

Was it helpful?

Solution

It seems to be a bug in IE, as many (or all) console elements that should be functions appear to be objects instead.

If you're trying to call function methods that aren't there, then you might want to refer to this article: http://whattheheadsaid.com/2011/04/internet-explorer-9s-problematic-console-object

Otherwise the simplest solution is do:

typeof(console.log) !== 'undefined'

It's not the prettiest solution as it's really a bug with IE failing standards compliance in spite of their drive to do the opposite, but console.log shouldn't really be anything other than an object or function so it should be safe to use. Otherwise you could do something more complex like:

switch (typeof(console.log)) {
    case 'object':
    case 'function':
        // Should be a valid console.log object, do something with it
    break;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top