Symbol Keys Hide From Object.keys And JSON

Symbol property keys stay invisible to Object.keys, for..in and JSON.stringify, and only getOwnPropertySymbols or Reflect.ownKeys can enumerate them.

JavascriptData Integrity

Code

javascriptsnippet
19 lines
// Symbol property keys are invisible to every enumeration mechanism by default: Object.keys, for..in, and JSON.stringify all miss them
const id = Symbol('id');
const record = { name: 'adapter', [id]: 42 };

console.log('Object.keys:', JSON.stringify(Object.keys(record)));
console.log('JSON.stringify:', JSON.stringify(record));

// Listing Symbol keys explicitly: only these two APIs can see them
console.log(
  'own symbols:',
  Object.getOwnPropertySymbols(record)
    .map((s) => s.description)
    .join(','),
);
console.log('Reflect.ownKeys entries:', Reflect.ownKeys(record).length);

// This is why libraries hang internal metadata on Symbols: serialization never leaks it
record[id] = 43;
console.log('symbol value still readable:', record[id]);

Output

Captured from a sandboxed run of this snippet in the build pipeline.

Object.keys: ["name"]
JSON.stringify: {"name":"adapter"}
own symbols: id
Reflect.ownKeys entries: 2
symbol value still readable: 43

How it works

Top-level bindings id, record hold the demo state — nothing sits inside a function scope.

Module-level object literal, fixed keys: name.

Applying it

The run emits 5 labeled sections — Object.keys:, JSON.stringify:, own symbols:, Reflect.ownKeys entries:, symbol value still readable: — one per step.

Statements that matter

.map((s) => s.description)
.join(','),
);

Interactive Tools for This Topic

FAQ

Is anything beyond the standard library required?
Nothing beyond the standard library — the snippet is dependency-free. Its state lives in module-level bindings: `id`, `record`.