LearnFrenzy
Sign in Take a free test

Objects · Predict the Output · Question 17

this in a nested function and an arrow Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 17 Back to Exercise

What does the following JavaScript code print (console output, as in Node.js)?

main.js
const timer = {
  seconds: 10,
  start() {
    function inner() { return this === timer; }
    const arrow = () => this === timer;
    return [inner(), arrow()];
  },
};
console.log(timer.start());
  • A [ true, true ]
  • B [ false, false ]
  • C [ false, true ] Correct
  • D [ true, false ]
Correct Answer: Option C

inner() is a plain call, so it gets its own this (the global object here). The arrow function has no this of its own and uses start's this, which is timer.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links