Lingua-e
JavaScript

What is a Strict Mode: 'this' is undefined?

← Errors

In strict mode, a regular function called without an explicit receiver has 'this' set to undefined instead of the global object. This is the correct strict mode behavior, but it breaks code that accidentally relied on 'this' being the global object.

Why it happens

  • A regular function called as a plain function (not as a method) in strict mode.
  • A callback passed to setTimeout or an event listener loses its 'this' binding.
  • ES modules are automatically in strict mode, so all functions in them follow this rule.
  • A class method extracted and called as a plain function.

Examples

JavaScript

Code that triggers the error

"use strict";

function greet() {
  console.log(this.name); // 'this' is undefined
}
greet();

Error output

TypeError: Cannot read properties of undefined (reading 'name')

How to fix it

Use arrow functions for callbacks to inherit 'this' from the enclosing scope. Use .bind(this) to explicitly fix the context. Avoid relying on 'this' being the global object; it is bad practice even in sloppy mode.

Fixed code

"use strict";

const obj = { name: "Alice" };

// Option 1: call with explicit context
greet.call(obj);

// Option 2: use an arrow function inside a method
const person = {
  name: "Alice",
  greet() {
    const inner = () => console.log(this.name); // arrow inherits 'this'
    inner();
  },
};

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