0

Why is

for (var i in window) {
    console.log(i);
}

interpreted differently from

for (var i in window) {
    console.log(window[i]);
}

? When I ran the first script, I got the window object's properties (https://www.w3schools.com/jsref/obj_window.asp) and its methods. However, when I ran the second script, I got the window's objects and functions. Why is console.log(i) different from console.log(window[i])?

Swangie
  • 175
  • 8
  • 2
    The first outputs the property name, the second outputs the property value. It's called "square bracket notation" – evolutionxbox Jan 20 '21 at 22:34
  • @evolutionxbox Okay, thank you - can you make that an official answer so I can mark it as correct? – Swangie Jan 20 '21 at 22:36
  • Does this answer your question? [How to iterate over a JavaScript object?](https://stackoverflow.com/questions/14379274/how-to-iterate-over-a-javascript-object) or [Iterate through object properties](https://stackoverflow.com/questions/8312459/iterate-through-object-properties) – evolutionxbox Jan 20 '21 at 22:40

0 Answers0