Symbols as Object Keys
Use symbols for non-colliding properties.
Symbols as Object Keys is a free JavaScript Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Symbols as Keys
A symbol can be used as an object property key. Because each symbol is unique, such a property cannot accidentally collide with any string key.
const id = Symbol("id")
const user = {}
user[id] = 123
console.log(user[id])Computed Symbol Keys
To use a symbol as a key in an object literal, wrap it in square brackets as a computed property name.
const id = Symbol("id")
const user = {
name: "Ann",
[id]: 99
}
console.log(user[id])Reading a Symbol Property
You must hold a reference to the same symbol to read its property. Without that reference, the value is effectively hidden.
const secret = Symbol("secret")
const obj = { [secret]: "hidden" }
console.log(obj[secret])Not Enumerable in for-in
Symbol keys are skipped by a for-in loop. This keeps symbol properties out of ordinary iteration.
const tag = Symbol("tag")
const obj = { a: 1, b: 2, [tag]: 3 }
for (const key in obj) {
console.log(key)
}Skipped by Object.keys
Object.keys returns only string keys, so symbol-keyed properties are excluded from it as well.
const tag = Symbol("tag")
const obj = { a: 1, [tag]: 2 }
console.log(Object.keys(obj))Hidden From JSON
JSON.stringify ignores symbol keys entirely, so they never appear in serialized output. This makes them useful for internal metadata.
const meta = Symbol("meta")
const obj = { name: "Bo", [meta]: "internal" }
console.log(JSON.stringify(obj))Finding Symbol Keys
To list symbol keys, use Object.getOwnPropertySymbols, which returns an array of the symbol properties on an object.
const tag = Symbol("tag")
const obj = { a: 1, [tag]: 2 }
const syms = Object.getOwnPropertySymbols(obj)
console.log(syms.length)Avoiding Name Clashes
Symbol keys let different libraries add properties to the same object without risk of overwriting each other, since their symbols differ.
const libA = Symbol("data")
const libB = Symbol("data")
const shared = {}
shared[libA] = 1
shared[libB] = 2
console.log(shared[libA], shared[libB])Symbol Keys and Spread
The object spread operator does copy symbol keys, unlike for-in and Object.keys, so be aware they carry over when cloning.
const tag = Symbol("tag")
const src = { a: 1, [tag]: 2 }
const copy = { ...src }
console.log(copy[tag])Storing Private-Like Data
Symbol keys offer a soft form of privacy: the data is reachable only by code holding the symbol, keeping it out of normal property listings.
const balance = Symbol("balance")
function makeAccount() {
const acct = {}
acct[balance] = 100
return { acct: acct, key: balance }
}
const a = makeAccount()
console.log(a.acct[a.key])Deleting Symbol Properties
You remove a symbol property the same way as any other, using the delete operator with the symbol reference.
const tag = Symbol("tag")
const obj = { [tag]: 5 }
delete obj[tag]
console.log(obj[tag])Quick Check
Test your symbol key knowledge.
Recap
Recap: Symbols make safe, hidden object keys.
- Use square brackets for computed symbol keys
- Skipped by for-in, Object.keys, and JSON
- Listed via Object.getOwnPropertySymbols
- Great for clash-free metadata
const k = Symbol("k")
const o = { [k]: 7 }
console.log(o[k])Frequently asked questions
Is the “Symbols as Object Keys” lesson free?
Yes — the full text of “Symbols as Object Keys” is free to read here on the web, and the JavaScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Symbols as Object Keys”?
Use symbols for non-colliding properties. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start JavaScript Academy?
No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Symbols as Object Keys” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this JavaScript Academy lesson?
Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Creating Unique Symbols
- Symbols as Object Keys
- The Global Symbol Registry
- Well-Known Symbols