Your product could be here, in front of thousands of JavaScript developers. Sponsor this spot →
JavaScript Quiz

Is a var loop variable accessible after a for loop?

Do you know what will be the output of this code?

JavaScript
for (var i = 0; i < 3; i++) {}
console.log(i);
Show answer and explanation

The answer is 3.

var is scoped to the function (or the global scope), not the loop block. The loop stops when i < 3 is false, which is when i is 3.

With let i, the variable would only exist inside the loop and this would throw a ReferenceError.

Take the full quiz Next question →

More Basics questions

  1. What happens when you reassign a const in JavaScript?
  2. Why is typeof null "object" in JavaScript?
  3. What is the difference between == and === in JavaScript?
  4. What does Array.push() return in JavaScript?
  5. Why does "5" * "2" equal 10 in JavaScript?

Related topics