Look at a piece of code first
var arr = [2,3,4,5]; for (var i = 0; i < arr.length; i++) { Console.log (i, "type:" +typeof i,arr[i]); } for (Var j in arr) { Console.log (J, "type:" +typeof j,arr[j]); }
Results
The standard for-loop i is the number type, which represents the subscript of the array, but I in the Foreach loop represents the array's key is the string type.
However: if we expand the array on the basis of the above code, just add the function and see what the following results are
var arr = [2,3,4,5]; Array.prototype.remove = function (val) {console.log ("the deletion method added in the array!");} ; for (var i = 0; i < arr.length; i++) { Console.log (i, "type:" +typeof i,arr[i]); } for (Var j in arr) { Console.log (J, "type:" +typeof j,arr[j]); }
The results are as follows:
The result of the output of the for...in is more than one line, the extra line is our extended function, not the value we define in the array
So when we're traversing the array, it's best not to use for...in
It is best not to use for...in to iterate through the array in JavaScript