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
5414 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:
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
Latest Issues
while loop
From 1970-01-01 08:00:00
0
0
0
An error occurred while accessing the login page
From 1970-01-01 08:00:00
0
0
0
java - Questions about While,break
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template