LearnFrenzy
Sign in Take a free test

Scope and Hoisting · Predict the Output · Question 1

Which level does show() read? Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 1 Back to Exercise

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

main.js
const level = "global";
function show() {
  return level;
}
function run() {
  const level = "local";
  return show();
}
console.log(run());
  • A local
  • B undefined
  • C global Correct
  • D ReferenceError
Correct Answer: Option C

Scope is lexical: show() was written at the top level, so it reads the top-level level, no matter that run() calls it.

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