Sub topic 1
What Gets Iterated: Own Enumerable Properties
When you loop over an object with Object.keys, Object.values, or Object.entries, you don't get every property the object can reach. You get a filtered set: the object's own, enumerable, string-keyed properties. This chapter explains what each of those three words means, so the later chapters' loops don't surprise you.
Prerequisites
This chapter assumes you already know:
- JavaScript variables, functions, and arrow functions
- Object literals and property access with dot and bracket notation
- Arrays and the
for...ofloop - Basic array methods such as
forEach,map, andfilter
You don't need any prior knowledge of prototypes or property descriptors. The chapter introduces both as far as iteration needs them.
Own versus Inherited Properties
Every object has a prototype, which is another object it falls back to when a property lookup fails. A property stored directly on the object is an own property. A property found by following the prototype chain is inherited.
Plain object literals inherit from Object.prototype. That is why {} has a toString method even though you never defined one. Iteration methods would be useless if they listed toString, hasOwnProperty, and the rest on every object. So Object.keys and its relatives look only at own properties.
Object.hasOwn(obj, key) tells you whether a key is own:
const base = { greet() { return 'hi'; } };
const user = Object.create(base);
user.name = 'Ada';
console.log(user.greet()); // 'hi' (found through the prototype)
console.log(Object.hasOwn(user, 'name')); // true
console.log(Object.hasOwn(user, 'greet')); // false
console.log(Object.keys(user)); // ['name']Enumerable versus Non-Enumerable Properties
Each property carries hidden attributes stored in a property descriptor. One of these attributes is enumerable, a boolean that marks whether the property should appear when someone lists the object's properties.
Properties you create the usual way, with an object literal or by assignment, are enumerable. Properties created with Object.defineProperty are non-enumerable unless you set enumerable: true yourself. Most built-in methods, such as Object.prototype.toString, are non-enumerable.
const user = { name: 'Ada' };
Object.defineProperty(user, 'id', { value: 7 }); // enumerable defaults to false
console.log(Object.getOwnPropertyDescriptor(user, 'name'));
// { value: 'Ada', writable: true, enumerable: true, configurable: true }
console.log(Object.getOwnPropertyDescriptor(user, 'id'));
// { value: 7, writable: false, enumerable: false, configurable: false }
console.log(user.id); // 7 (still readable)
console.log(user.propertyIsEnumerable('id')); // false
console.log(Object.keys(user)); // ['name']A non-enumerable property still exists and you can still read it. Listing methods just skip it.
String Keys versus Symbol Keys
Property keys are either strings or symbols. A symbol is a unique value created with Symbol(), often used for keys that shouldn't clash with ordinary ones. Object.keys, Object.values, and Object.entries return only string-keyed properties. Number keys such as obj[1] count as strings, because JavaScript converts them to '1'. A later chapter shows how to retrieve symbol keys when you need them.
The Three Filters Together
A property shows up in Object.keys only if it passes all three checks:
| Property | Own | Enumerable | String key | In `Object.keys`? |
|---|---|---|---|---|
| `name` set by literal | Yes | Yes | Yes | Yes |
| `greet` on prototype | No | Yes | Yes | No |
| `id` via `defineProperty` | Yes | No | Yes | No |
| `[Symbol('tag')]` | Yes | Yes | No | No |
| `toString` from `Object.prototype` | No | No | Yes | No |
Why This Matters
These rules are why Object.keys works well for data. You get exactly the fields you stored, without methods inherited from prototypes or internal bookkeeping hidden as non-enumerable properties. JSON.stringify and the object spread syntax { ...obj } follow similar rules, so an object behaves the same way whether you loop over it, copy it, or serialize it.
Exercise
Given const proto = { a: 1 }; const obj = Object.create(proto); obj.b = 2; Object.defineProperty(obj, 'c', { value: 3 }); obj[Symbol('d')] = 4; obj[5] = 6;, what does Object.keys(obj) return, and why is each other property excluded?
Show answer
['5', 'b']. The number key 5 becomes the string '5' and is listed first because integer-like keys come before other string keys. a is excluded because it is inherited from proto. c is excluded because defineProperty made it non-enumerable. Symbol('d') is excluded because it is a symbol key.
Exercise
Write one expression that returns true only if key is an own, enumerable property of obj.
Show answer
Object.hasOwn(obj, key) && obj.propertyIsEnumerable(key). propertyIsEnumerable already returns false for inherited properties, so obj.propertyIsEnumerable(key) on its own also works, as long as obj inherits from Object.prototype. Use Object.prototype.propertyIsEnumerable.call(obj, key) if it might not.