Lingua-e
← Errors
JavaScript

Illegal Invocation: What It Means and How to Fix It

Error reference

Illegal Invocation

Language

JavaScript

Severity

Medium

When it happens

This TypeError is thrown when a built-in method is called with the wrong 'this' context.

Potential fixes

Use .bind() to preserve the correct 'this': const log = console.log.bind(console). Wrap the method in an arrow function. When passing methods as callbacks, always bind them to their host object first.

Deep dive

This TypeError is thrown when a built-in method is called with the wrong 'this' context. Many native browser and Node.js methods use 'this' internally and will throw if detached from their host object.

Why it happens

  • Storing a built-in method (like console.log) in a variable and calling it without the original receiver.
  • Passing a DOM method as a callback where 'this' becomes undefined or window.
  • Using Function.prototype.call() or .apply() on a native method with the wrong context.

Potential fixes

Use .bind() to preserve the correct 'this': const log = console.log.bind(console). Wrap the method in an arrow function. When passing methods as callbacks, always bind them to their host object first.

Examples

JavaScript

Code that triggers the error

const log = console.log;
log("hello");

Error output

TypeError: Illegal invocation

Fixed code

// Bind the method to its original object
const log = console.log.bind(console);
log("hello"); // works

// Or use an arrow wrapper
const log2 = (...args) => console.log(...args);

Practice in English

How would you explain an Illegal Invocation to a fellow dev? Choose the right phrase:

"I hit an error...

Ready to practice your English at work?

Lingua-e has interactive exercises built around real developer conversations: standups, code reviews, retrospectives, and more. Practice until it comes naturally.

Try Lingua-e for free