Sub topic 4
Where Symbol Keys Show Up and Where They Stay Hidden
This chapter assumes you know how to create symbols and use them as computed property keys, as covered in the earlier chapters. It also assumes you have used Object.keys, for...in, and JSON.stringify on ordinary objects.
Symbol-keyed properties are stored on the object like any other property. Most of the everyday tools for listing keys filter them out, though. To work with symbol keys safely, you need to know which APIs skip them and which ones return them.
The APIs that skip symbol keys
Code written before ES2015 had no idea symbols would exist. To keep that code working, the older key-listing APIs return only string keys:
Object.keys,Object.values, andObject.entriesfor...inloopsObject.getOwnPropertyNames, which skips symbols even though it returns non-enumerable string keysJSON.stringify
const id = Symbol('id');
const user = { name: 'Ada', [id]: 42 };
console.log(user[id]); // 42
console.log(Object.keys(user)); // ['name']
console.log(Object.entries(user)); // [['name', 'Ada']]
for (const key in user) console.log(key); // name
console.log(Object.getOwnPropertyNames(user)); // ['name']
console.log(JSON.stringify(user)); // {"name":"Ada"}Because of this filtering, you can attach metadata to an object without breaking code that loops over its keys or serializes it.
The APIs that return symbol keys
Two reflection APIs list symbol keys:
Object.getOwnPropertySymbols(obj)returns only the symbol keys.Reflect.ownKeys(obj)returns every own key, both strings and symbols.
Both APIs ignore enumerability, so they return non-enumerable keys as well.
const id = Symbol('id');
const tag = Symbol('tag');
const user = { name: 'Ada', [id]: 42 };
Object.defineProperty(user, tag, { value: 'admin', enumerable: false });
console.log(Object.getOwnPropertySymbols(user)); // [Symbol(id), Symbol(tag)]
console.log(Reflect.ownKeys(user)); // ['name', Symbol(id), Symbol(tag)]Reflect.ownKeys uses a fixed order. Integer-like keys come first in ascending order, then string keys in insertion order, then symbol keys in insertion order.
Copying keeps symbol keys
Object copying does not follow the same rule as key listing. Object.assign and object spread both copy own enumerable symbol-keyed properties:
const id = Symbol('id');
const original = { name: 'Ada', [id]: 42 };
const spread = { ...original };
const assigned = Object.assign({}, original);
console.log(spread[id]); // 42
console.log(assigned[id]); // 42Non-enumerable symbol properties are not copied, just as non-enumerable string properties are not.
JSON drops symbols in two ways
JSON.stringify drops symbols when they are keys and also when they are values. When a symbol is a value, the result depends on where the value sits:
const s = Symbol('x');
console.log(JSON.stringify({ a: s, b: 1 })); // {"b":1}
console.log(JSON.stringify([s, 1])); // [null,1]
console.log(JSON.stringify(s)); // undefinedThe data loss happens silently. If you send symbol-keyed data over the network or store it, you have to convert it to string keys yourself first.
Quick reference
| API | String keys | Symbol keys | Includes non-enumerable |
|---|---|---|---|
| `Object.keys` / `for...in` | Yes | No | No |
| `Object.getOwnPropertyNames` | Yes | No | Yes |
| `Object.getOwnPropertySymbols` | No | Yes | Yes |
| `Reflect.ownKeys` | Yes | Yes | Yes |
| `JSON.stringify` | Yes | No | No |
| Spread / `Object.assign` | Yes | Yes | No |
Exercise
An object is created with const k = Symbol('k'); const o = { a: 1, [k]: 2 };. What do Object.keys({ ...o }).length, Reflect.ownKeys({ ...o }).length, and JSON.stringify([k]) produce?
Show answer
They produce 1, 2, and "[null]". Spread copies the enumerable symbol key, but Object.keys ignores symbol keys, so it counts only a. Reflect.ownKeys counts both a and k. Inside an array, JSON.stringify turns a symbol value into null.
Exercise
You need to list every own property of an object, including symbol keys and non-enumerable keys, in a single call. Which API do you use?
Show answer
Use Reflect.ownKeys(obj). It returns all own string and symbol keys whether or not they are enumerable.