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!