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]); }

9th Sep 2021, 6:21 AM
XPEHO3ABP
XPEHO3ABP - avatar
2 Antworten
+ 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); }
9th Sep 2021, 6:25 AM
A͢J
A͢J - avatar
+ 1
Потому что итерация проходится по массиву
9th Sep 2021, 1:24 PM
Shadoff
Shadoff - avatar