singularity

Symbol


const sym = Symbol();

const obj = {};
obj[sym] = "hello";

console.log(obj[sym]); // hello


const sym = Symbol();

const obj = {
  [sym]: "hello"
};

console.log(obj[sym]); // hello

Symbols and privacy


const name = Symbol();

const person = {
  [name]: "John Doe"
};

console.log(person.name); // undefined
console.log(person["name"]); // undefined
console.log(person[name]); // John Doe


const name = Symbol();

const person = {
  [name]: "John Doe",
  age: 20
};

console.log(Object.getOwnPropertyDescriptors(person));
/*
{
  age: { value: 20, writable: true, enumerable: true, configurable: true },
  [Symbol()]: {
    value: 'John Doe',
    writable: true,
    enumerable: true,
    configurable: true
  }
}
*/


console.log(Object.getOwnPropertySymbols(person));
// [ Symbol() ]

Adding a description to symbols

Symbol.toPrimitive

Symbol.toStringTag

Symbol.isConcatSpreadable