Does IE9 support console.log, and is it a real function?

mloughran picture mloughran · Mar 29, 2011 · Viewed 115.7k times · Source

In which circumstances is window.console.log defined in Internet Explorer 9?

Even when window.console.log is defined, window.console.log.apply and window.console.log.call are undefined. Why is this?

[Related question for IE8: What happened to console.log in IE8?.]

Answer

Andy E picture Andy E · Mar 29, 2011

In Internet Explorer 9 (and 8), the console object is only exposed when the developer tools are opened for a particular tab. If you hide the developer tools window for that tab, the console object remains exposed for each page you navigate to. If you open a new tab, you must also open the developer tools for that tab in order for the console object to be exposed.

The console object is not part of any standard and is an extension to the Document Object Model. Like other DOM objects, it is considered a host object and is not required to inherit from Object, nor its methods from Function, like native ECMAScript functions and objects do. This is the reason apply and call are undefined on those methods. In IE 9, most DOM objects were improved to inherit from native ECMAScript types. As the developer tools are considered an extension to IE (albeit, a built-in extension), they clearly didn't receive the same improvements as the rest of the DOM.

For what it's worth, you can still use some Function.prototype methods on console methods with a little bind() magic:

var log = Function.prototype.bind.call(console.log, console);
log.apply(console, ["this", "is", "a", "test"]);
//-> "thisisatest"