Guides

Why this is undefined in JavaScript — 3 causes and how to fix them

Illustration: the remote was carried to another room — you press the button, but there's no TV

The method worked great while you called it by hand: counter.increment(). Then you hooked it up to a button — and everything broke. The console says Cannot read properties of undefined. Or, sneakier: no error at all, but the screen shows NaN instead of a number.

You didn't touch the method's code. Only one thing changed — who called it. And here's JavaScript's big oddity: this inside a function is decided not where the function is written, but where it's called.

What this even is

this is a method's word for "me". When you write counter.increment(), the object to the left of the dot becomes this. The method says "my count" — and it means counter's count.

Now the key part. If there's no dot on the left, "me" gets lost. The method is the same, but it no longer has an owner.

Picture a TV remote. In the room with the TV it works. Carry it into another room — same buttons, but nothing to switch. The remote isn't broken. It just doesn't know which TV it belongs to.

Here are three cases where the dot quietly disappears. From most to least common.

Cause 1. The method was passed as a callback (most common)

You hand the method to someone who will call it later: a button, a timer, another function. That's called a callback.

const counter = {
  count: 0,
  increment() {
    this.count += 1
  }
}

button.addEventListener("click", counter.increment)

Look closely: counter.increment — no parentheses. You passed the function itself, not a call. The button will call it later on its own — and this becomes the button, not the counter. The button has no count, so you get undefined + 1, which is NaN. Same story with setTimeout, except this becomes the global window.

How to check. Put console.log(this) as the first line of the method. If you see the button, window or undefined instead of your object — that's it.

How to fix. Wrap the call in an arrow function so the dot is back where it belongs:

button.addEventListener("click", () => counter.increment())

The second way is to bind the owner permanently: counter.increment.bind(counter). Works the same, just reads worse.

In old class-based React components it's the same story: onClick={this.handleClick} passes the method without its owner. That's why in such code you'll often see a line in the constructor like this.handleClick = this.handleClick.bind(this). Looks like a spell, but the meaning is simple: "remember whose you are".

Cause 2. A regular function inside a method

Inside the method all is fine, but you write another function — say, for forEach:

class Cart {
  constructor() {
    this.items = [990, 450]
    this.total = 0
  }

  sum() {
    this.items.forEach(function (price) {
      this.total += price
    })
  }
}

This crashes with Cannot read properties of undefined (reading 'total'). The inner function has its own this. And forEach calls it without any dot. Inside a class, that means undefined.

How to check. The error points to a line inside the nested function, not in the method itself.

How to fix. Replace function with an arrow. An arrow function has no this of its own — it takes it from outside, from the method:

this.items.forEach((price) => {
  this.total += price
})

It's the same mechanism as with variables: an arrow "sees" the surroundings where it was written. More on that kind of visibility in what scope is.

Cause 3. An arrow where a method belongs

The opposite situation. Since arrows are so handy, people write them everywhere:

const cart = {
  items: [990, 450],
  count: () => this.items.length
}

And again, an error. The arrow doesn't take this from the object — it takes it from outside. And outside the object there's no cart.

How to check. The method is declared with => right inside the object.

How to fix. Write the object's method itself the regular way: count() { return this.items.length }. Keep arrows for functions inside methods — as in cause 2.

How to sort it out quickly next time

A two-line rule:

  • An object's method — the regular form name() {}.
  • A function inside a method, or a callback — an arrow () => {}.

If the error still has you stuck, take it apart piece by piece — as in how to read an error message. The line and the property name in parentheses almost always show where the owner got lost.

Why is this error rare in React components?

Modern components are functions, not classes, and they don't use this at all. A this error in React is a sign of old class-based code.

What is this in Python?

There, self plays that role, and Python spells it out explicitly as the method's first argument. So it's much harder to lose.

Can this be undefined without an error?

Yes. If you only read a property, you get a quiet undefined or NaN. The error appears only when you try to go deeper: this.user.name.

Become an AI architect — don’t just read about AI

The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.

About the Academy
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →