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

Why does an arrow function in setTimeout keep this?

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

JavaScript
const timer = {
  seconds: 10,
  start() {
    setTimeout(() => console.log(this.seconds));
  },
};

timer.start();
Show answer and explanation

The answer is 10.

The arrow function has no this of its own, so it uses the one from start(), which is timer. A regular function callback would lose it.

Take the full quiz Next question →

More this questions

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

Related topics