Using the var keyword in a for loop

This is due to "var" or not "var" in the "JavaScript-in-in" in JavaScript. loop? (but says more about the scope - this question does NOT apply to the area)

Is a loop more efficient / general for an object or an array?

Option 1 - Installing an External Loop

// Object
var x;
for (x in obj) { ... }
// Array
var i;
for (i = 0; i < array.length; ++i) { ... }

Option 2 - Setting var in a loop

// Object
for (var x in obj) { ... }
// Array
for (var i = 0; i < array.length; ++i) { ... }
+3
source share
2 answers

var gets elevated and bound to a function, not a block, so the differences will be optimized by the compiler.

The second is a little faster, because there are fewer characters per wire. This difference is not significant enough to be a factor in your decision about what to use.

+6

BACKWARDS ( , ). . ?

var i = arr.length; //or 10
while(i--)
{

}

var, , / reset / .

0

All Articles