-1

I have used this code same theory I have used but 1one work but 2nd does not work in JavaScript

var me_labba = {
  name: 'kezara',
  bd: '1999'
};
var str_walata_awlak_na = "sdasdasd"
let key;

//1st one
for (key of str_walata_awlak_na) {
  console.log(key);
}
//2nd one
for (key of me_labba) {
  console.log(key);
}
simo54
  • 218
  • 5
  • 16
kezara
  • 1
  • 2
    You cannot use `for...of` on plain objects. It only works on iterables like arrays or strings. – VLAZ Nov 13 '21 at 11:14
  • 1
    *"1one work but 2nd does not work"*: Use `for..in` for the second loop instead. In the first one you don't iterate *keys* -- the variable name is misleading -- those are *values* (characters) of the (iterable) string. – trincot Nov 13 '21 at 11:14

1 Answers1

1

Use for in with Objects in in Javascript

for (key in me_labba){
    console.log(key);
}

Or use Object.keys

Object.keys(me_labba).forEach((k) => console.log(k))
Nishanth
  • 304
  • 3
  • 12