for-loop does create a scope. In fact, every statement block is a scope in itself.
In your examples, just change the value of variable 'a' to 100 or 1000 and you will see the difference.
In your examples, just change the value of variable 'a' to 100 or 1000 and you will see the difference.
In fact, Crockford has mentioned this as well: http://javascript.crockford.com/code.html
"JavaScript does not have block scope, so defining variables in blocks can confuse programmers who are experienced with other C family languages."
Which is why this code works:
(function() {
for (var i=0; i<100; i++);
alert(i);
})();Notice the variable `i` is accessible from outside the for-loop.
Another thing, JavaScript 1.7 has introduced block level scopes.. you can achieve that by using the 'let' keyword instead of 'var' to declare a variable. Although most browsers don't support it yet.