Statement 1
var i = 0;
for (; i < 10 ; i++);
Statement 2
for (var i = 0 ; i < 10 ; i++);
Are those two statements equal?
var i = 0;
for (; i < 10 ; i++);
for (var i = 0 ; i < 10 ; i++);
Are those two statements equal?
Nope, no difference between the 2 examples, functionally.
However, statement 2 can cause confusion. This is because i
is not scoped to the for
block, it's accessible outside of that for
loop, which can result in a polluted global scope.
Just make sure you keep track of your variables when using them like statement 1.
Personally, I prefer something like this:
var i;
for(i = 0; i < 10; i++){
// Do stuff
}
for(i = 0; i < 20; i++){
// Do other stuff
}
This way, you'll always have your iterator properly set.
There are no specific differences. Javascript's variable's scope is different from other languages like C#. This is already discussed here