LearnFrenzy
Sign in Take a free test

Scope and Hoisting · Predict the Output · Question 17

The name of a class expression 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 Animal = class Pet {
  whoAmI() {
    return Pet.name;
  }
};
console.log(new Animal().whoAmI(), typeof Pet);
  • A Animal function
  • B Pet function
  • C ReferenceError: Pet is not defined
  • D Pet undefined Correct
Correct Answer: Option D

Like a named function expression, a named class expression binds its name only inside its own body. Outside, only Animal exists.

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