0

the difference (speed, performace, side effects, ...) between implementations of the for loop:

between

var i;
for(i = 0; i < length; i++){ //Do something}
// more code

and

for(var i = 0; i < length; i++){ //Do something}
// more code

and

for(i = 0; i < length; i++){ //Do something}
// more code

and between

var e;
for( e in array){ //Do something}
// more code

and

for(var e in array){ //Do something}
// more code

and

for(e in array){ //Do something}
// more code
jondavidjohn
  • 61,812
  • 21
  • 118
  • 158
rkmax
  • 17,633
  • 23
  • 91
  • 176

3 Answers3

2

There is no difference.

JavaScript variables have only function scope, and although you can place a var statement in the initialisation part of a for loop in reality the declaration is "hoisted" to the top of the scope such that when your second case examples run they're treated exactly like the first cases.

EDIT: Since you updated the question after I answered, the third syntax that you added where you don't use the var keyword means the variable i (or e) will be created as a global - unless it already exists as a global in which case the existing variable will be overwritten. Global variable access is slower than local variable access.

NOTE: my interpretation of the question is that it is not comparing a standard for loop with the for..in variant, it is just compared the different variable declaration methods with each other for a standard for loop, then doing the same again for a for..in loop.

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • Well, you are right that it doesn't really matter where one places the `var`, other than wasting bytes of code. BUT, the for...in, is not the same as the other for loop, there's situations in which one will work better than the other...PS: I didn't downvote...I swear XD (really) – JCOC611 Oct 31 '11 at 02:46
  • That answers half of the question (does not deserve a downvote, though) – Thilo Oct 31 '11 at 02:47
  • @JCOC611 - The question doesn't compare a standard for loop with a for..in, it compares different standard for loops with each other, and then (kind of in parallel) it compares different for..in loops with each other. Also the question was edited _after_ I posted my answer to introduce a third syntax not originally mentioned. So to whoever downvoted (especially without an explantion): that was pretty harsh. – nnnnnn Oct 31 '11 at 02:49
  • @nnnnnn: Ah I missed that...sorry. +1 :) – JCOC611 Oct 31 '11 at 02:56
  • @JCOC611 - well, maybe the way I read the question wasn't how it was actually intended but in any case thanks for balancing up the downvote with your +1. – nnnnnn Oct 31 '11 at 03:00
2

There is no difference in relation to the declariation of your counter variable..

BUT ALWAYS DECLARE YOUR VARIABLES WITH var

Otherwise they pollute javascript's already dirty global scope...

As far as for...in vs traditional for look here...

(Which is my answer to the duplicate question...)

Community
  • 1
  • 1
jondavidjohn
  • 61,812
  • 21
  • 118
  • 158
0

Yes, there is a difference between for loop and for/in loop in javascript. Here is what is different

consider this array

var myArr = ["a", "b", "c"];

Later, I add an element to this array, but in a different way, like so:

myArr[myArr.length + 1] = "d";

At this point of time, this is what the array looks like, if you console.log it

["c", "b", "a", undefined × 1, "d"]

Now, let us loop through the array using for and for/in loop and see what the difference is: first, lets try the for loop

for(var i = 0; i != myArr.length; i++) {     // note, i used " != " instead of " < ". Yes this is faster.
  console.log(myArr[i]);
}
// output will look like this:
// "a" 
// "b" 
// "c"
// undefined × 1
// "d" 
// - This still shows the undefined value in the array.

Now, lets look at the for/in loop

for(key in myArr) {
  console.log(myArr[key]);
}
// array will look like:
// "a"
// "b"
// "c"
// "d"
// This will skip the undefined value.

Difference 1: The javascript interpreter will skip all values that are null or undefined when using the for/in loop. Also, the for/in loop will convert all values if it encounters a primitive value, to its equivalent wrapper object. Where as the for loop doesn't do this.

Difference 2: When using the for loop, we declared the variable i within the loop, this variable will be scoped within a function, if the for loop is written within that function. Which means, until the function ends, the variable i is still available, even outside of the for loop, but within that function. Where as, in case of the for/in loop, the scope of the "key" variable dies immediately after the for/in loop stops executing, which means, there is less memory utilization.

kaizer1v
  • 898
  • 8
  • 20