for...in

Iterates over the enumerable string-keyed property names of an object.

Since ES1 Spec ↗

Syntax

for (const key in object) {
  // ...
}

Examples

const obj = { a: 1, b: 2 };
for (const key in obj) {
  console.log(key);
}
Output
a
b
const obj = { x: 10, y: 20 };
for (const key in obj) {
  console.log(key, obj[key]);
}
Output
x 10
y 20
const obj = { a: 1 };
for (const key in obj) {
  if (Object.hasOwn(obj, key)) {
    console.log("own:", key);
  }
}
Output
own: a

Notes

- `for...in` also visits inherited enumerable properties; guard with `Object.hasOwn()` when you only want own properties. - Do not use `for...in` to iterate arrays; use `for...of` or array methods to preserve order and skip non-index keys.

Browser & runtime support

EnvironmentSince version
chrome 1.0
firefox 1.0
safari 1.0
edge 12
node 0.10

See also