ScanSkill

for...in

The for…in loop is used to loop over objects.

Syntax

for (let property in object) {
  // Statements
}

The for...in iterates over each property of an array, the property is present in the variable declared inside the small brackets of the for...in loop.

Example

const object = { a: 1, b: 2, c: 3 };

for (const property in object) {
  console.log(`${property}: ${object[property]}`);
}

/* expected output:
a: 1
b: 2
c: 3
*/

The for…in loop loops over, objects it gives out the key of the objects while looping over the object. It can also loop over arrays, but for…of loop is a better choice for looping over arrays.