How does it for a loop determine when to break the loop

I ran into another type of loop that I don't see or use normally. I tried to figure it out, but got even more confused in the process. It doesn't have a third argument or even a test method to break the loop. Then it iterates over the array and prints its value. In fact it encounters an "undefined" value for a specific index, but I am not telling it to abort when it encounters undefined.please, help me break the puzzle here ...

(function () {
    var a = [1, 2, 3, 4, 5, 6, 7, 8, 9];
    for (var i = 0, value; value = a[i++];) {
        document.write(value + '</br>');

Run codeHide result


source to share

5 answers

In javascript, when you access array elements outside of the length of the array, you don't get a range check error, a return value undefined

that matches false

when processed as a boolean - thus terminating the loop when the end of the array is reached.

If any of the array elements are equal undefined

or any other value that becomes false

boolean, the loop will end on that element.

The assignment operator in javascript returns the left side value, so the expression is value = a[i++]

used to return the value a[i]

and increment i

is in that order. If this value is converted to false

boolean, the loop ends.



All loop arguments are optional.

The first statement in the for loop is just a variable declaration, so you can define multiple variables. Instead, the author could write:

var a=[1,2,3,4,5,6,7,8,9];
var value;

for(var i = 0; value = a[i++];)


but went for brevity.

The third operator (increment / decrement) is optional, the author (again for the sake of absolute brevity) decided to use postfix increment ( i++

I will return THEN, increase it, while ++i

increasing THEN, return a value with an incremental value).

They could also write this:

(function () {
    var a = [1, 2, 3, 4, 5, 6, 7, 8, 9];
    var value;
    var i = 0;
    for ( ; value = a[i++]; ) {
        document.write(value + '</br>');


Finally, a[a.length+1]

which will evaluate the final condition, returns undefined

what is false and will terminate the loop.



A few notes:

  • undefined

    is the value "false" (incorrect)
  • The loop ends when its condition becomes invalid
  • Destination a[i]

    (or a[i++]

    ) on value


So when i == 9

, a[i++] == undefined

so value == undefined

, so the cycle is completed.



To repeat the loop until a condition after the first ;

is true. In your case, eventually after the last element of the array a[i++]

becomes false ( ToBoolean(undefined)

). As soon as this happens, the cycle stops.

Take a look at the Specification Statement :

  1. Repeat
        a. If the first expression is present, then I am. Let testExprRef be the result of evaluating the first expression.
            II. If ToBoolean (GetValue (testExprRef)) is false , return (normal, V, empty).


for (var i = 0, value; value = a[i++];) {


the second part of the for is evaluated as a condition for each iteration.

the automatic type conversion happens here so that the value is calculated a[i++]

. if a[i++]

true, the loop continues, unless it stops.



All Articles