Enumerability and Property Visibility
You'll learn how a property's enumerable flag controls whether it appears in loops and certain Object methods.
What Does "Enumerable" Mean?
Not all properties are created equal. Some properties are enumerable (visible in loops and listings), while others are non-enumerable (hidden from most common operations). This is controlled by the enumerable flag in a property's descriptor, which you learned about earlier.
Think of enumerable properties like items displayed in a store window — they're visible when you browse. Non-enumerable properties are like items in a back storage room — they exist, but you won't see them unless you specifically ask for them.
How Enumerability Affects Your Code
When a property is enumerable:
- It appears in
for...inloops - It's included in
Object.keys() - It's included in
Object.entries()andObject.values()
When a property is non-enumerable:
- It's skipped by
for...inloops - It's excluded from
Object.keys(),Object.entries(), andObject.values() - It's still accessible directly with dot or bracket notation
- It appears in
Object.getOwnPropertyNames()
Why This Matters
JavaScript's built-in properties (like methods on Array prototypes) are non-enumerable by default. This prevents them from cluttering your loops. When you create properties normally, they're enumerable. But you can make them non-enumerable using Object.defineProperty() with enumerable: false.
This visibility control lets you add "internal" or "metadata" properties to objects without affecting code that iterates over properties.
Key Takeaway: Enumerable properties are visible in loops and Object.keys(), while non-enumerable properties are hidden from these operations but still accessible directly — giving you control over what users of your objects typically see.