Sub topic 8
Listing Properties and Their Order
This chapter assumes you can create objects with literals and computed keys, read properties with dot and bracket notation, and check whether a property exists with Object.hasOwn. You should also be comfortable with for...of loops over arrays.
JavaScript gives you several ways to list an object's properties. They differ in two ways: which properties they include, and what form they return them in. The order they use follows fixed rules that you can predict.
The Four Common Tools
Object.keys, Object.values, and Object.entries each return a plain array. Object.keys returns the keys, Object.values returns the values, and Object.entries returns [key, value] pairs. The for...in loop works differently. It walks the keys one at a time and also visits inherited properties.
const o = { b: 1, 2: "two", a: 2, 1: "one" };
console.log(Object.keys(o)); // ["1", "2", "b", "a"]
console.log(Object.values(o)); // ["one", "two", 1, 2]
for (const [k, v] of Object.entries(o)) {
console.log(k, v);
}
// 1 one
// 2 two
// b 1
// a 2Keys always come back as strings, even when you wrote them as numbers. The key 2 in the literal becomes "2" in the output.
The Ordering Rules
The output above is not in the order the properties were written. Own string keys are listed in two groups:
- Integer-like keys (such as
"0","7","42") come first, in ascending numeric order. - All other string keys follow, in the order they were added.
A key counts as integer-like only if it is the canonical form of a non-negative integer. "01" and "-1" are not integer-like, so they keep insertion order with the other strings.
const scores = { "10": "a", "9": "b", "01": "c", "-1": "d", name: "e" };
console.log(Object.keys(scores)); // ["9", "10", "01", "-1", "name"]"9" sorts before "10" because the comparison is numeric, not alphabetical.
What Gets Skipped
None of the four common tools return symbol keys or non-enumerable properties. Use Object.getOwnPropertySymbols to get symbol keys, or Reflect.ownKeys to get every own key, enumerable or not, with symbols listed last.
const id = Symbol("id");
const user = { name: "Lin", [id]: 101 };
console.log(Object.keys(user)); // ["name"]
console.log(Object.getOwnPropertySymbols(user)); // [Symbol(id)]
console.log(Reflect.ownKeys(user)); // ["name", Symbol(id)]for...in adds inherited enumerable properties after the object's own properties. If you want only own keys, filter them with Object.hasOwn:
const base = { inherited: 1 };
const child = Object.create(base);
child.own = 2;
for (const k in child) {
console.log(k, Object.hasOwn(child, k));
}
// own true
// inherited falseThe prototype chapter later in this tutorial explains where inherited properties come from.
| Tool | Returns | Inherited | Symbols | Non-enumerable |
|---|---|---|---|---|
| `Object.keys` | array of keys | no | no | no |
| `Object.values` | array of values | no | no | no |
| `Object.entries` | array of `[key, value]` | no | no | no |
| `for...in` | one key per iteration | yes | no | no |
| `Reflect.ownKeys` | array of keys | no | yes | yes |
Common Errors
- Expecting numeric keys to keep the order you wrote them in. They are sorted.
- Using
for...inon an object that inherits enumerable properties, then processing those properties by accident. - Comparing keys to numbers with
===.Object.keys({ 1: "x" })[0] === 1isfalsebecause the key is"1". - Expecting
Object.keysto report symbol-keyed metadata.
Exercise
What does this print? const p = { z: 1, 3: "c", y: 2, "1": "a", [Symbol("s")]: 0 }; console.log(Object.keys(p));
Show answer
["1", "3", "z", "y"]. The integer-like keys "1" and "3" come first in ascending order. "z" and "y" follow in insertion order. The symbol key is omitted because Object.keys does not return symbols.
Exercise
You loop over an object with for...in and see a key that you never assigned to the object. What is the likely cause, and how do you exclude it?
Show answer
The key is an enumerable property inherited from the object's prototype. Skip it with if (!Object.hasOwn(obj, k)) continue; or iterate over Object.keys(obj), which lists only own properties.