LearnFrenzy
Sign in Take a free test

Scope and Hoisting · Predict the Output · Question 11

A variable inside an IIFE Answer, explanation & discussion

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

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

main.js
var count = 5;
(function () {
  var count = 1;
  count++;
})();
console.log(count);
  • A 2
  • B 6
  • C 1
  • D 5 Correct
Correct Answer: Option D

The IIFE declares its own count, which shadows the outer one and disappears when the function returns.

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