topic description
Array inJS is of variable length. If there is no defined value, it defaults to undefined
.related codes
var arr = [0,1,2];
arr[10]=10;
arr.filter(function(x){
return x===undefined
})
expect results
in the title, only arr is set to [0jue 1jue 2], and then set arr [10] = 10, at this time the arr should be equal to [0meme 1je 2je undefined 7 arr 10], and then Filter operation on this array, it will return an item equal to undefined, that is, the return should be [undefined*7]. But the result of the actual operation is an empty function [], and then output x, that is,
, inside the filter.var arr = [0,1,2];
arr[10]=10;
arr.filter(function(x){
console.log(x);
return x===undefined
})
it is found that it will only output 0 ~ 1 ~ 2 ~ 10.
it seems that there is no explanation about this on MDN, so I hope God can help answer it. You will save a broken heart in the hot water. Thank you very much.