Why is it showing "undefined"??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why is it showing "undefined"???

let obj = {a: 1, b: 2, c: 3}; for( let key in obj ){ console.log(obj.key); } Why , undefined?

25th Apr 2020, 3:16 PM
Lucifer
Lucifer - avatar
2 Answers
+ 3
With (for/in) loops you need to us the bracket notation to retrieve the value from the object. https://code.sololearn.com/WKtS9CO2tvXf/?ref=app
25th Apr 2020, 7:48 PM
ODLNT
ODLNT - avatar
+ 3
let obj = {a: 1, b: 2, c: 3}; for(let val of Object.values(obj)){ console.log(val); } (You can do the same with keys.)
25th Apr 2020, 4:07 PM
HonFu
HonFu - avatar