for - in

Available in ES 2009.
The for in statement loops through the properties of an Object:

for (key in object) { 
  // do something
}

The "key" is a string and not a number.
This is for looping over iterables.
Iterators exist on maps, arrays and sets.


Iterating an Object

This traverses all enumerable properties of an object
Iterates over the property keys of an object
This cannot iterate through a collection

var myobject = { 
   name: "Steve"
   age: 20
};
for (var propertyVal in myobject) {
   write(propertyVal);
   write(myobject[propertyVal]);
}

Iterating an Array

Arrays > Loops


© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrevNext