Programming Search⌘K

Featurecore

Traversing object properties

Also called for...in, Object.keys, Object.values, Object.entries, Object.fromEntries, Reflect.ownKeys, iterating over an object, property order.

JavaScript walks an object's properties with for...in, which includes inherited ones, or with Object.keys, Object.values and Object.entries, which give own enumerable ones. Integer keys come first.

Language
JavaScript

Synopsis

const scores = { ada: 9, alan: 7 };
for (const key in scores) console.log(key, scores[key]);
for (const [name, score] of Object.entries(scores)) console.log(`${name}=${score}`);
console.log(Object.keys(scores), Object.values(scores));
Output
ada 9
alan 7
ada=9
alan=7
[ 'ada', 'alan' ] [ 9, 7 ]
Form Gives Inherited Non-enumerable Symbol keys
for (const k in obj) keys, one at a time yes no no
Object.keys(obj) an array of keys no no no
Object.values(obj) an array of values no no no
Object.entries(obj) an array of [key, value] pairs no no no
Object.getOwnPropertyNames(obj) an array of keys no yes no
Reflect.ownKeys(obj) an array of keys no yes yes

Semantics

r1. "The for...in statement iterates over all enumerable string properties of an object (ignoring properties keyed by symbols), including inherited enumerable properties."1 Example: basics.

r2. Object.entries "returns an array of a given object's own enumerable string-keyed property key-value pairs." "This is the same as iterating with a for...in loop, except that a for...in loop enumerates properties in the prototype chain as well."2 Object.values "returns an array of a given object's own enumerable string-keyed property values."3 Reflect.ownKeys "returns an array of the target object's own property keys", and "It is the only way to get all own properties" with string and symbol keys "in one call".4 Example: basics.

r3. The order of own keys is fixed by ECMA-262. OrdinaryOwnPropertyKeys lists the keys that are array indices "in ascending numeric index order", then the other String keys "in ascending chronological order of property creation", then the Symbol keys "in ascending chronological order of property creation".5 MDN: for for...in, "Within each component of the prototype chain, all non-negative integer keys (those that can be array indices) will be traversed first in ascending order by value, then other string keys in ascending chronological order of property creation."1 Example: order.

r4. for...in walks up the prototype chain. MDN: "If multiple objects in the prototype chain have a property with the same name, only the first one will be considered, and it is only visited if it's enumerable."1 ECMA-262 has the same rule: "a property of a prototype is not processed if it has the same name as a property that has already been processed".6 Example: shadow.

r5. Changing the object during the loop is risky. ECMA-262: "A property that is deleted before it is processed by the iterator's next method is ignored. If new properties are added to the target object during enumeration, the newly added properties are not guaranteed to be processed in the active enumeration."6 MDN: "In general, it is best not to add, modify, or remove properties from the object during iteration, other than the property currently being visited."1 Example: added.

r6. Object.fromEntries "transforms a list of key-value pairs into an object."7 With Object.entries it turns an object into pairs, so array methods can change the pairs before they go back into an object. Example: transform.

Examples

Integer keys first, then strings, then symbols (rule r3).

const o = { b: 1, 10: "x", a: 2, 2: "y", [Symbol("s")]: 3, "-1": "z" };
console.log(Object.keys(o));
console.log(Reflect.ownKeys(o));
Output
[ '2', '10', 'b', 'a', '-1' ]
[ '2', '10', 'b', 'a', '-1', Symbol(s) ]

-1 is not an array index, so it keeps its place among the string keys.

A non-enumerable own property hides an inherited one of the same name (rule r4).

const proto = { a: 1, b: 2 };
const o = Object.create(proto);
Object.defineProperty(o, "a", { value: 3 });
o.c = 4;
const keys = [];
for (const k in o) keys.push(k);
console.log(keys, o.a);
Output
[ 'c', 'b' ] 3

Properties added during the loop (rule r5).

const o = { a: 1, b: 2 };
const visited = [];
for (const k in o) {
  visited.push(k);
  o[k + k] = 0;
}
console.log(visited, Object.keys(o));
Output
[ 'a', 'b' ] [ 'a', 'b', 'aa', 'bb' ]

Changing every value with entries and fromEntries (rule r6).

const prices = { tea: 2, cake: 5 };
const doubled = Object.fromEntries(Object.entries(prices).map(([k, v]) => [k, v * 2]));
console.log(doubled);
Output
{ tea: 4, cake: 10 }

Versions

Feature Edition
order of own property keys ECMAScript 20158
Object.values, Object.entries ECMAScript 20179
Object.fromEntries ECMAScript 20199
order of inherited keys in for...in ECMAScript 20208

MDN: "The order was first defined for own properties only in ECMAScript 2015; ECMAScript 2020 defines order for inherited properties as well."8 The TC39 list of finished proposals gives 2017 for Object.values/Object.entries, 2019 for Object.fromEntries and 2020 for for-in mechanics.9

Pitfalls

Using for...in on an array

This code runs but gives a wrong answer on purpose.

const arr = [10, 20];
arr.extra = "x";
const seen = [];
for (const i in arr) seen.push(i);
console.log(seen, typeof seen[0]);
for (const v of arr) console.log(v);
Output
[ '0', '1', 'extra' ] string
10
20

"Array indexes are just enumerable properties with integer names", and "the for...in loop will return all enumerable properties, including those with non–integer names and those that are inherited."1 MDN suggests a numeric for loop, forEach or for...of, "because they will return the index as a number instead of a string, and also avoid non-index properties."1

Expecting insertion order for integer keys

This code runs but gives a wrong answer on purpose.

const byId = {};
byId[30] = "c";
byId[10] = "a";
byId[20] = "b";
console.log(Object.values(byId));
const m = new Map([
  [30, "c"],
  [10, "a"],
  [20, "b"],
]);
console.log([...m.values()]);
Output
[ 'a', 'b', 'c' ]
[ 'c', 'a', 'b' ]

Keys that are array indices come first in ascending order, whatever order they were added in (rule r3). "A Map object iterates entries, keys, and values in the order of entry insertion."8 See Maps.

Comparison

In Python, an object's own attributes are in __dict__, "A dictionary or other mapping object used to store an object’s (writable) attributes."10 Iterating over it gives only what the instance holds, much like Object.keys, while for...in also visits the enumerable properties of the prototypes (rule r1).

References

  1. MDN Web Docs, JavaScript Reference, for...in, the introduction; Description; Deleted, added, or modified properties; Array iteration and for...in
  2. MDN Web Docs, JavaScript Reference, Object.entries(), the introduction; Description
  3. MDN Web Docs, JavaScript Reference, Object.values(), the introduction
  4. MDN Web Docs, JavaScript Reference, Reflect.ownKeys(), the introduction; Description
  5. ECMAScript 2026 Language Specification (ECMA-262, 17th edition), clause 10, 10.1.11.1 OrdinaryOwnPropertyKeys
  6. ECMAScript 2026 Language Specification (ECMA-262, 17th edition), clause 14, 14.7.5.9 EnumerateObjectProperties
  7. MDN Web Docs, JavaScript Reference, Object.fromEntries(), the introduction
  8. MDN Web Docs, JavaScript Reference, Map, the introduction; Key Order
  9. TC39, Finished Proposals, Object.values/Object.entries, Object.fromEntries, for-in mechanics, Expected Publication Year
  10. The Python Language Reference, Data model, Python 3.14, 3.2.11.1 Special attributes

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source