There are two ways to traverse arrays in js
Under normal circumstances, the results of the above two methods of traversing arrays are the same. First let’s talk about the first difference between the two
The i in the standard for loop is of type number, which represents the subscript of the array, but the i in the foreach loop represents that the key of the array is of type string, because everything in js is an object. Try it yourself alert(typeof i); This difference is a minor problem. Now that I add the following code, the above execution results will be different.
Two more suggestions
1. Do not use for in to traverse the array, use the standard for loop variable array (we cannot guarantee whether the js we introduce will use prototype to extend the native Array)
2. If you want to extend the native classes of js, don’t use prototype