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

while loop method to traverse array

一个新手
Release: 2017-09-25 10:09:01
Original
5343 people have browsed it

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
    */
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template