Home >Web Front-end >JS Tutorial >while loop method to traverse array

while loop method to traverse array

一个新手
一个新手Original
2017-09-25 10:09:015359browse

I found this code when looking at the jQuery source code. I tried it myself and simply recorded it.

var arr = [ 'a', 'b', 'c', 'd' ],
    i = 0;while( arr[i++] ){    
    console.log( arr[i] );
    /*
    输出 b c d undefined
    */
}

The code execution sequence is:
(1) Determine whether while(arr[i]) exists, if it exists, execute (2) (3)
(2) i++
(3) console.log( arr[i] )

So, it can be written as console.log( arr[i-1] )

The above is the detailed content of while loop method to traverse array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn