EN
JavaScript - do loops check the array.length every time when comparing i to array.length?
1 answers
0 points
Do normal loops check the array.length
each time?
For example a simple for
loop:
xxxxxxxxxx
1
var i, length;
2
3
for (i = 0, length = array.length; i < length; i++) {
4
// ...
5
}
1 answer
0 points
Yes, the length
property of an array is checked at each enumeration when the loop is constructed as follows: for (var i = 0; i < array.length; ++i)
(which is a shorthand for your implementation).
Explanation
The for loop consists of three parts which are executed as follows:
xxxxxxxxxx
1
for (part1; part2; part3) {
2
// ...
3
}
part1
is executed before the enumeration,part2
is a condition to test before each loop iteration,part3
is an expression after each enumeration.
See also
References
0 commentsShow commentsAdd comment