Iterate over an object in JavaScript

javascript

Updated August 29, 2026

Use Object.entries() when you need an object's own enumerable keys and values:

const settings = { theme: 'dark', compact: true };

for (const [key, value] of Object.entries(settings)) {
  console.log(key, value);
}

Use Object.keys() for keys and Object.values() for values. A for...in loop also sees enumerable properties inherited through the prototype chain, so guard it when that is intended:

for (const key in settings) {
  if (Object.hasOwn(settings, key)) {
    console.log(key, settings[key]);
  }
}

These methods do not recursively walk nested objects. For nested data, write a traversal that matches the data model or use a schema-aware library. Do not mutate an object while relying on the order of keys unless your algorithm explicitly supports it.

Sources

related.

Ruslan Osipov

Ruslan Osipov

About the author