LearnFrenzy
Sign in Take a free test

Scope and Hoisting · Predict the Output · Question 8

A var declared in a nested function Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 8 Back to Exercise

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

main.js
function outer() {
  function inner() {
    var secret = 42;
  }
  inner();
  return typeof secret;
}
console.log(outer());
  • A number
  • B undefined Correct
  • C 42
  • D ReferenceError
Correct Answer: Option B

secret belongs to inner()'s function scope. outer cannot see it, and typeof returns "undefined" for an unresolvable name instead of throwing.

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