+ 1

For of loops for window object in js is not iterable .explain please.

I have used for... of loop on string it works But when I applied it on window object it console an error that window object is not iterable. How both the objects are different ?? As we know string is also an object in js.

22nd Aug 2020, 9:15 AM
Divya Mohan
Divya Mohan - avatar
11 Réponses
+ 3
You should use the for/in loop to iterate over the window object. If you want the value of the window properties, use the computed property ex. window[prop]. https://code.sololearn.com/WVwWpz69ll8Z/#js
22nd Aug 2020, 10:46 AM
ODLNT
ODLNT - avatar
+ 4
Can you show your code please.
22nd Aug 2020, 9:21 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 4
Try now may be this will be work replace your line 16 var myobjt = Object.create(window) ; for(var key in myobjt){ console.log(key);
22nd Aug 2020, 9:47 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
Strings are not objects, they are primitive type but strings, as do most of the other primitives, have wrappers objects that allow the primitives to be access and manipulated in some form or fashion. references- https://developer.mozilla.org/en-US/docs/Glossary/Primitive https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
22nd Aug 2020, 10:39 AM
ODLNT
ODLNT - avatar
22nd Aug 2020, 9:23 AM
Divya Mohan
Divya Mohan - avatar
0
But I want to get value of object not key Hence I am using for of loop
22nd Aug 2020, 9:33 AM
Divya Mohan
Divya Mohan - avatar
0
But I want the value from window object not from myarray array.
22nd Aug 2020, 9:37 AM
Divya Mohan
Divya Mohan - avatar
0
🗡️ Ulduz 🗡️ using For in loop we can only get the key of object not value of object. Try for in and for of in case of string both are not same.in case of string key is index and value is character. Similarly I want to get key and value for window object.
22nd Aug 2020, 9:52 AM
Divya Mohan
Divya Mohan - avatar
0
It works for singelton object better
22nd Aug 2020, 10:19 AM
Divya Mohan
Divya Mohan - avatar
0
I have just read every data in js is an object . But now I think it is more right to say everything in js act like object. Or hey ODLNT am I still wrong?? Please answer if you aren't agree with this above statement.
22nd Aug 2020, 10:48 AM
Divya Mohan
Divya Mohan - avatar