LearnFrenzy
Sign in Take a free test

Scope and Hoisting · Predict the Output · Question 3

A var inside an if (false) block Answer, explanation & discussion

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

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

main.js
function test() {
  console.log(v);
  if (false) {
    var v = 10;
  }
  console.log(v);
}
test();
  • A undefined undefined Correct
  • B ReferenceError
  • C undefined 10
  • D 10 10
Correct Answer: Option A

The declaration is hoisted during the creation phase even though the block never runs, so v exists as undefined. The assignment never happens.

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