LearnFrenzy
Sign in Take a free test

Scope and Hoisting · Predict the Output · Question 20

Three functions reading one variable name Answer, explanation & discussion

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

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

main.js
var a = "A";
function one() {
  var a = "B";
  function two() {
    function three() {
      return a;
    }
    return three();
  }
  return two();
}
console.log(one(), a);
  • A A A
  • B B B
  • C B A Correct
  • D undefined A
Correct Answer: Option C

three has no a, nor does two; the search reaches one(), which declares a = "B". The global a stays "A".

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