JavaScript · Middle · Question 4 of 75

What does this code print after a second?

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

Explanation

The timer calls the callback without a context, so this isn't timer. An arrow function () => console.log(this.seconds) would take this from start and print 10. This is the classic case where arrows are useful.

Further reading

Problem with a question

JavaScript · Middle · question 4

Test yourself

Take the JavaScript · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz