Your product could be here, in front of thousands of JavaScript developers. Sponsor this spot →
JavaScript Quiz

Why does an arrow function method return undefined for this?

Do you know what will be the output of this code?

JavaScript
const obj = {
  value: 42,
  regular() {
    return this.value;
  },
  arrow: () => this.value,
};

console.log(obj.regular(), obj.arrow());
Show answer and explanation

The answer is 42 undefined.

regular is called as obj.regular(), so this is obj. The arrow function has no this of its own. It uses the this of the surrounding scope, which isn’t obj.

Use regular method syntax for object methods.

Take the full quiz Next question →

More this questions

  1. Why does this become undefined when you extract a method?
  2. Can you change this on a bound function with call()?
  3. Why does an arrow function in setTimeout keep this?
  4. What is the difference between call and apply in JavaScript?
  5. What happens when a constructor returns an object?

Related topics