Course contentsShow
← JavaScript
Lesson 643 of 2,72315. ObjectsFree lesson

Symbol Properties Are Excluded by Default

Object.keys/values/entries ignore symbol-keyed properties; use Object.getOwnPropertySymbols().

Symbol Properties Are Excluded by Default

You'll learn why symbols stay hidden during normal object iteration and how to find them when needed.

When you iterate over object properties or retrieve keys with methods like Object.keys(), Object.values(), or Object.entries(), JavaScript intentionally skips symbol-keyed properties. This is by design—symbols were created to add "private-like" metadata that won't interfere with regular property operations.

Why Symbols Stay Hidden

Think of symbols like secret notes written in invisible ink on an object. Normal property tools (Object.keys(), for...in, etc.) are like regular reading glasses—they only see ordinary text (string keys). You need special "UV light" tools to reveal symbol properties.

The Special Tool: Object.getOwnPropertySymbols()

When you do need to access symbol-keyed properties, use Object.getOwnPropertySymbols(). This method returns an array containing only the symbol keys:

const id = Symbol('id');
const user = {
  name: 'Alice',
  age: 30,
  [id]: 12345
};

console.log(Object.keys(user));              // ['name', 'age']
console.log(Object.values(user));            // ['Alice', 30]
console.log(Object.entries(user));           // [['name', 'Alice'], ['age', 30]]

// Symbol properties require special retrieval
console.log(Object.getOwnPropertySymbols(user)); // [Symbol(id)]
console.log(user[id]);                       // 12345

Notice how the symbol property [id] is completely invisible to the standard methods but accessible when you specifically look for symbols.

Key Takeaway: Symbol-keyed properties are excluded from Object.keys(), Object.values(), Object.entries(), and for...in loops by default—use Object.getOwnPropertySymbols() to retrieve them explicitly.