Ad Space — Top Banner

Illegal invocation

JavaScript Programming Language

Severity: Moderate

What Does This Error Mean?

Illegal invocation means you called a function but the 'this' context inside that function was wrong. Many built-in browser functions (like console.log or document.getElementById) must be called with the correct 'this' — they only work when called on the object they belong to. If you save one of these functions to a variable and call it separately, 'this' loses its connection to the original object and the error occurs.

Affected Models

  • All browsers
  • Node.js
  • All JavaScript environments

Common Causes

  • Saving a built-in method to a variable and calling it standalone — like: const log = console.log; log('hello')
  • Passing a built-in method as a callback where it loses its 'this' context
  • Using .call() or .apply() with the wrong 'this' context on a native function
  • Attempting to call a non-function value — like calling a string or number as if it were a function
  • Using a browser API method (like focus() or click()) after detaching it from the DOM element

How to Fix It

  1. If you saved a method to a variable, bind it to the original object: const log = console.log.bind(console); log('hello'); — now it works correctly.

    .bind(console) locks the 'this' context to console, so the function always knows it belongs to console.

  2. When passing a method as a callback, wrap it in an arrow function to preserve the context: setTimeout(() => console.log('hello'), 1000) instead of setTimeout(console.log, 1000)

    Arrow functions do not change 'this' — they capture the context from where they are written, which is usually what you want.

  3. If calling a DOM method like element.focus() on a saved reference, make sure the element is still attached to the document and that you are calling it on the element object, not a copy.

    const myFocus = element.focus; myFocus(); — this loses context. Use element.focus() directly, or const myFocus = element.focus.bind(element);

  4. If using .call() or .apply() with a native method, pass the correct host object as the first argument.

    Example: Array.prototype.slice.call(arrayLike, 0) — the first argument to .call() sets the 'this' context.

  5. Check that the variable you are calling is actually a function. Use typeof myVar === 'function' before calling it.

    Sometimes a variable that should be a function is actually undefined, null, or another type — calling it directly causes 'is not a function' or 'illegal invocation'.

When to Call a Professional

Illegal invocation is always fixable. The most common fix is to bind the function to the correct object before using it as a callback, or to wrap it in an arrow function. This is a normal JavaScript gotcha that trips up even experienced developers.

Frequently Asked Questions

What does 'this' mean in JavaScript?

'this' refers to the object that a function belongs to at the time it is called. When you call console.log('hello'), 'this' inside log is the console object. If you save log to a variable and call it standalone, 'this' is no longer console — it is either undefined (in strict mode) or the global object. Built-in functions need the correct 'this' to work.

What is the difference between .bind(), .call(), and .apply()?

.bind() creates a new function with 'this' permanently locked to the object you specify. .call() calls the function immediately with a specific 'this' and individual arguments. .apply() calls the function immediately with a specific 'this' and arguments as an array. For callbacks, .bind() is most useful because it creates a reusable bound version.

Why does this work in a script but break inside a callback?

Because when a function is passed as a callback, it loses its connection to the original object. Inside a callback, 'this' is often the global object or undefined, not the object the method came from. The fix is to use .bind() or an arrow function wrapper to preserve the correct 'this'.