Course contentsShow
← JavaScript
Lesson 1007 of 2,72323. SymbolsFree lesson

Retrieving Symbol Properties

Learn how to access symbol-keyed properties using bracket notation and Object.getOwnPropertySymbols().

Retrieving Symbol Properties

You'll learn how to access symbol-keyed properties and discover all symbols on an object.

When you use a symbol as an object key, it doesn't show up in normal property loops or methods like Object.keys(). Symbols are deliberately hidden from typical enumeration—but you can access them when you need to.

Accessing a Known Symbol

If you have the symbol itself, use bracket notation just like accessing any computed property:

const id = Symbol('userId');
const user = {
  name: 'Alice',
  [id]: 42
};

console.log(user[id]); // 42

You must use brackets—dot notation won't work because symbols aren't strings.

Finding All Symbol Properties

To discover which symbols are on an object, use Object.getOwnPropertySymbols(). This method returns an array of all symbol keys:

const email = Symbol('email');
const phone = Symbol('phone');

const contact = {
  name: 'Bob',
  [email]: 'bob@example.com',
  [phone]: '555-1234'
};

const symbols = Object.getOwnPropertySymbols(contact);
console.log(symbols); // [Symbol(email), Symbol(phone)]

// Now access their values:
symbols.forEach(sym => {
  console.log(contact[sym]); 
  // 'bob@example.com', then '555-1234'
});

Analogy: Symbols are like VIP passes—regular entry methods (Object.keys(), for...in) can't see them, but if you have the pass itself or ask security (getOwnPropertySymbols()) for the VIP list, you get full access.

Key Takeaway: Access symbol-keyed properties with bracket notation using the symbol reference, and discover all symbols on an object with Object.getOwnPropertySymbols().