TypeError: Illegal Invocation on console.log.apply

26,993

It may not work in cases when execution context changed from console to any other object:

This is expected because console.info expects its "this" reference to be console, not window.

console.info("stuff")
stuff
undefined
console.info.call(this, "stuff")
TypeError: Illegal invocation
console.info.call(console, "stuff")
stuff
undefined

This behavior is expected.

https://bugs.chromium.org/p/chromium/issues/detail?id=48662

Share:
26,993
Jacksonkr
Author by

Jacksonkr

Another guy in a chair with questions & answers. jacksonkr.com

Updated on July 27, 2022

Comments

  • Jacksonkr
    Jacksonkr almost 2 years

    If you run this in the chrome console:

    console.log.apply(null, [array])
    

    Chrome gives you back an error:

    // TypeError: Illegal Invocation
    

    Why? (Tested on Chrome 15 via OSX)

  • C B
    C B over 10 years
    If you need to use as a function, you can use console.info.bind(console)
  • mucaho
    mucaho almost 9 years
    so can you use console.info.call(console, "stuff") in all browsers that support ES5?
  • PeterM
    PeterM about 8 years
    Same goes for apply: console.info.apply(console, arguments)
  • Alan C. S.
    Alan C. S. almost 8 years
    Same argument applies to other functions such as console.log() and document.writeln(). So, always provide the correct execution context if using call() or apply(). Alternately, use bind() as @JohnWilliams has pointed out.
  • Benny Bottema
    Benny Bottema about 7 years
    This is still applicable to IE11/Edge when the DevTools F12 are not open.