Object.getOwnPropertySymbols()
You'll learn how to discover all symbol-keyed properties directly attached to an object.
What Are Symbol-Keyed Properties?
Remember when we learned about computed keys with symbols? Symbols create unique property keys that don't show up in normal property listings. They're invisible to Object.keys(), Object.values(), and even for...in loops. But sometimes you need to find them.
How Object.getOwnPropertySymbols() Works
This method returns an array of all symbol keys that belong directly to an object (not inherited ones). It's the symbol equivalent of Object.getOwnPropertyNames(), which finds string-keyed properties.
const mySymbol = Symbol('id');
const anotherSymbol = Symbol('secret');
const user = {
name: 'Alice',
[mySymbol]: 12345,
[anotherSymbol]: 'hidden'
};
const symbols = Object.getOwnPropertySymbols(user);
console.log(symbols); // [Symbol(id), Symbol(secret)]
// Access the values using these symbols
console.log(user[symbols[0]]); // 12345
Why This Matters
Think of symbols as hidden compartments in a drawer. Object.keys() shows you the visible items, but Object.getOwnPropertySymbols() reveals the secret compartments. This is useful when you need to inspect all properties (including private-ish ones) or when working with libraries that use symbols for metadata.
const obj = {
visible: 'everyone sees this'
};
obj[Symbol('internal')] = 'hidden data';
console.log(Object.keys(obj)); // ['visible']
console.log(Object.getOwnPropertySymbols(obj)); // [Symbol(internal)]
Key Takeaway: Use Object.getOwnPropertySymbols() to retrieve an array of all symbol-keyed properties directly on an object—the only way to discover properties that normal enumeration methods skip.