0
0
JavascriptHow-ToBeginner · 3 min read

How to Iterate Over Object in JavaScript: Simple Methods Explained

To iterate over an object in JavaScript, you can use the for...in loop to access each key, or use Object.keys(), Object.values(), or Object.entries() with forEach or for...of loops to get keys, values, or both. These methods let you process each property of the object easily.
📐

Syntax

Here are common ways to iterate over an object:

  • for...in: loops over all enumerable keys.
  • Object.keys(obj): returns an array of keys.
  • Object.values(obj): returns an array of values.
  • Object.entries(obj): returns an array of [key, value] pairs.

You can combine these with forEach or for...of loops to process each item.

javascript
for (const key in obj) {
  // use obj[key]
}

Object.keys(obj).forEach(key => {
  // use key and obj[key]
});

for (const value of Object.values(obj)) {
  // use value
}

for (const [key, value] of Object.entries(obj)) {
  // use key and value
}
💻

Example

This example shows how to use for...in and Object.entries() to print keys and values of an object.

javascript
const person = {
  name: 'Alice',
  age: 30,
  city: 'New York'
};

console.log('Using for...in loop:');
for (const key in person) {
  console.log(key + ': ' + person[key]);
}

console.log('\nUsing Object.entries():');
for (const [key, value] of Object.entries(person)) {
  console.log(key + ': ' + value);
}
Output
Using for...in loop: name: Alice age: 30 city: New York Using Object.entries(): name: Alice age: 30 city: New York
⚠️

Common Pitfalls

Common mistakes when iterating over objects include:

  • Using for...in without hasOwnProperty, which may include inherited properties.
  • Assuming order of keys is guaranteed (JavaScript objects do not guarantee key order).
  • Trying to use forEach directly on objects (it works only on arrays).

Always check if the property belongs to the object itself when using for...in.

javascript
const obj = Object.create({ inherited: 'yes' });
obj.own = 'no';

// Wrong: includes inherited property
for (const key in obj) {
  console.log(key); // outputs 'own' and 'inherited'
}

// Right: check own properties only
for (const key in obj) {
  if (obj.hasOwnProperty(key)) {
    console.log(key); // outputs 'own' only
  }
}
Output
own inherited own
📊

Quick Reference

MethodDescriptionReturns
for...inLoops over all enumerable keys (including inherited)Keys as strings
Object.keys(obj)Returns own enumerable keysArray of keys
Object.values(obj)Returns own enumerable valuesArray of values
Object.entries(obj)Returns own enumerable [key, value] pairsArray of [key, value] arrays

Key Takeaways

Use for...in loop with hasOwnProperty to avoid inherited keys.
Object.keys(), Object.values(), and Object.entries() return arrays for easy iteration.
forEach works on arrays, so use it with Object.keys/values/entries results.
JavaScript object key order is not guaranteed; don't rely on it.
Always check property ownership when iterating with for...in.