Sub topic 3
Enumerable Properties: What for...in Sees and Skips
This chapter assumes you know how to create objects, read properties with dot and bracket access, and write a basic for...in loop. It explains why some properties never show up in a for...in loop even though you can read them.
Every property has an enumerable flag
A JavaScript property has a value and also a few hidden attributes. One of them is enumerable. for...in visits a property only if two things are true: its key is a string, and its enumerable flag is true.
Properties you create with ordinary assignment or object literals are enumerable. Properties you create with Object.defineProperty are non-enumerable unless you say otherwise. Many built-in properties are non-enumerable too, such as array methods and an array's length.
const user = { name: 'Ada' };
Object.defineProperty(user, 'id', { value: 42 }); // enumerable defaults to false
user[Symbol('secret')] = 'hidden';
for (const key in user) {
console.log(key);
}
// name
console.log(user.id); // 42
console.log(Object.getOwnPropertyDescriptor(user, 'id'));
// { value: 42, writable: false, enumerable: false, configurable: false }
console.log(Object.getOwnPropertyNames(user)); // ['name', 'id']
console.log(Object.getOwnPropertySymbols(user)); // [Symbol(secret)]
console.log(user.propertyIsEnumerable('name')); // true
console.log(user.propertyIsEnumerable('id')); // falseThe id property exists and can be read, but the loop skips it. The symbol-keyed property is skipped as well. for...in never visits symbol keys, even when they are enumerable.
What for...in sees
| Property kind | Visited by for...in? |
|---|---|
| String key, created by assignment or literal | Yes |
| String key, `defineProperty` with no `enumerable: true` | No |
| Symbol key, any flag | No |
| Built-in methods such as `Array.prototype.map` | No |
| Class methods | No |
Class methods stay out of the loop
Methods defined in a class body are non-enumerable. Fields assigned in the constructor are ordinary enumerable properties.
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
describe() {
return `(${this.x}, ${this.y})`;
}
}
for (const key in new Point(1, 2)) {
console.log(key);
}
// x
// yMethods on the prototype of an object built with a constructor function work differently. If you assign them with Point.prototype.describe = ..., they are enumerable. The next chapter covers inherited properties and how to filter them.
Exercise
What does this code log? const o = { a: 1 }; Object.defineProperty(o, 'b', { value: 2, enumerable: true }); Object.defineProperty(o, 'c', { value: 3 }); o[Symbol('d')] = 4; for (const k in o) console.log(k);
Show answer
It logs "a" and then "b". The property c is non-enumerable because defineProperty defaults enumerable to false. The symbol key d is skipped because for...in never visits symbol keys.
Exercise
Why does for (const k in [1, 2]) not log "length" or "map"?
Show answer
length is an own non-enumerable property of the array. map lives on Array.prototype, and like the other built-in methods it is non-enumerable. for...in visits only enumerable string keys.