JavaScript For In Loop

Last Updated : 22 Jan, 2026

The for...in loop in JavaScript is used to iterate over the enumerable properties of an object. It provides an easy way to access each key (property name) one by one.

  • Iterates over object property names (keys) rather than values.
  • Commonly used for looping through objects, not arrays.
  • Can access property values using bracket notation inside the loop.
JavaScript
const car = {
    make: "Toyota",
    model: "Corolla",
    year: 2020
};

for (let key in car) {
    console.log(`${key}: ${car[key]}`);
}

Syntax:

for (key in object) {    // Code }

The for...in loop can also works to iterate over the properties of an array, but it is not recommended. for..in is mainly suitable for objects.

For arrays, we should use below loops.

  • For of Loop if we need to put continue or break in the loop
  • forEach() if we need execute something for all elements without any condition
JavaScript
// Example of for in for arrays
// Not a recommended way to traverse
// an array
const a = [1, 2, 3, 4, 5];

for (const i in a) {
  	console.log(a[i]);
}

Important Facts About for in Loop

  • The for...in loop is not recommended for use with arrays if maintaining index order is important.
  • The order of iteration in for...in loop is implementation-dependent, means the array elements may not be accessed in the expected sequence.
  • The order in which properties are iterated may not match the properties that are defined in the object.
Comment

Explore