Reputation: 67260
I have seen the following style of writing a for loop in JavaScript a number of times:
for(var i=0, n=myArray.length; i<n; i++) {
// do something
}
what is the advantage of that over the following?
for(var i=0; i<myArray.length; i++) {
// do something
}
Upvotes: 0
Views: 50
Reputation: 943214
In theory, it only has to look up the length of myArray
once, instead of each time it goes around the loop.
In practise, these days I believe browsers have JS engines smart enough to optimise the performance impact of that look up away.
Upvotes: 3