1

I've seen Javascript code that has used two different ways of defining a for loop.

for (var i=0;i < x.length; i++)

But it's also been

for (i=0; i < x.length; i++)

The same thing has happened with for-in loops

for (var i in x)

and

for (i in x)

Is there any difference between declaring i as a var and just saying i? Are there advantages of doing one over the other? Is one these right way to do this? From what I can tell, they both act the same, but there has to be some difference.

Note: I'm not asking about the difference between for-in and for (i=0)

michaelpri
  • 3,521
  • 4
  • 30
  • 46

4 Answers4

3

Without a var declaration somewhere in a function, references to i will be to the i property of the global object. This risks all sorts of unpredictable behavior if code in the body of the for loop invokes code (say, inside a called method) that also modifies the global i.

Note that declaring var i in the for loop initialization

for (var i = ...)

is equivalent to declaring var i; before the for loop:

var i;
for (i = ...)

In particular, the declaration of i will be hoisted to the top of the enclosing scope.

EDIT: If you enable strict mode, then you must declare your loop variables (all variables, actually). Referencing a variable that has not been declared with a var statement will result in a ReferenceError being thrown (rather than resulting in a global variable coming into existence).

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
1

JavaScript is functionally scoped. At least for now, the language does not have block level variables.

When you write for (var i=0; ... ); it is the same as

var i;
for (i=0; ... );

In the absence of "use strict"; variable declaration will be hoisted.

for (i=0; ... ); alone implies that i belongs to the global (top most) object (in browsers it's window)

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
  • Even in strict mode, variable declarations are hoisted; what strict mode does is cause a `ReferenceError` to be thrown when an undeclared variable is referenced. – Ted Hopp Mar 29 '15 at 00:59
1

Intro:

The for statement creates a loop that consists of three optional expressions

Source: Mozilla JavaScript Docs

Background:

You are referring to initialization, which is an expression or variable declaration. It's almost always used to initialize a counter variable that allows us to iterate through a collection as you've shown.

This expression may optionally declare new variables with the var keyword. These variables are not local to the loop, i.e. they are in the same scope the for loop is in. The result of this expression is discarded.

In JavaScript, variables can hold different data types, and in the case of a counter variable, JavaScript treats the variable as a number.

Answer:

The reason one can optionally declare a new variable (or not at all, as you've highlighted) is due to the nature of the JavaScript programming language. You've hit upon an important aspect of the language that deals with variables and scope.

"To var or not to var"

Please see this other post about using var or not using it at all to understand more...

What is the function of the var keyword and when to use it (or omit it)?

Community
  • 1
  • 1
0

If you say start using "i" without saying "var" first, you should have declared the variable before your for loop. Like:

var i;
for (i = 0; i < x; i++) {
  doSomething();
}

==OR==
  
for(var i = 0; i < x; i++){
  doSomething();  
}