0
what's wrong?
why does 16 come out instead of undefined? let arr = [2,8,16]; for(let key of arr){ console.log(arr[key]); }
2 Answers
+ 4
XPEHO3ABP
because 'of' gives value and 'in' gives key
so arr[key] = arr[2] = 16 and rest are undefined.
Do this:
let arr = [2,8,16];
for(let key in arr){
//console.log(key);
console.log(arr[key]);
}
for(let val of arr){
//console.log(key);
console.log(val);
}
+ 1
Потому что итерация проходится по массиву