JavaScript For…in Loop

The for…in loop in JavaScript is used to iterate over the enumerable properties of an object. The loop processes the object’s properties and assigns the current property’s key to a variable on each iteration.


for (variable in object) {


The variable is a different property name assigned to the variable on each iteration.

The Object is one whose non-Symbol enumerable properties are iterated over.


let appObj = { a: 21, b: 22, c: 23 };

for (const prop in appObj) {
  console.log(`appObj.${prop} = ${appObj[prop]}`);

We defined one Object and iterated its properties using for…in statement. 


Javascript For In Loop Tutorial With Example

The for…in loop iterates over the properties of an object in an arbitrary order. If the property is modified in one iteration and then visited at a later time, its value in the loop is its value at that later time.

The property deleted before it has been visited will not be visited later. Properties added to an object over which iteration occurs may either be visited or omitted from iteration.

for…in should not be used to iterate over an Array where the index order is essential.

Array indexes are just enumerable properties with integer names and are otherwise identical to the general object properties. There is no guarantee that the for… statement will return the indexes in any particular order.

The for…in loop statement will return all enumerable properties, including those with the non–integer names and those inherited.

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.