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.
Examples
Code that triggers the error
const log = console.log;
log("hello");Error output
TypeError: Illegal invocation
How to fix it
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.
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);Related errors
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