Symbol Unique Keys in JavaScript
Level 5308 keystrokes
Symbols create unique property keys that don't appear in regular object enumeration methods.
JavaScript code
// Create unique symbol for ID1const ID = Symbol("id");// Create unique symbol for SECRET2const SECRET = Symbol("secret");// Object with regular and symbol properties3const user = { // Regular string property4 name: "Alice", // Symbol property - hidden from most iterations5 [ID]: 12345, // Another symbol property6 [SECRET]: "abc123"7};// Access regular property8console.log(user.name);// Access symbol property using bracket notation9console.log(user[ID]);// Object.keys only shows regular keys10console.log(Object.keys(user));// Get all symbol properties11console.log(Object.getOwnPropertySymbols(user));// Use the first symbol (ID) as a key: 1234512console.log(user[Object.getOwnPropertySymbols(user)[0]]);Output
Alice 12345 [ 'name' ] [ Symbol(id), Symbol(secret) ] 12345
Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this JavaScript code and how often they appear.
(× 10)× 10.× 9;× 8"× 8[× 5]× 5:× 3=× 3,× 2