LearnFrenzy
Sign in Take a free test

Scope and Hoisting · Predict the Output · Question 9

Updating a global from a function Answer, explanation & discussion

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

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

main.js
var total = 10;
function update() {
  total = 20;
  var local = 5;
}
update();
console.log(total, typeof local);
  • A 10 undefined
  • B 20 number
  • C 10 number
  • D 20 undefined Correct
Correct Answer: Option D

update() has no local total, so the assignment changes the outer one. local is a var of update() and is not visible outside.

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