Symbol
Symbol is a primitive type introduced in ES6. Every symbol is guaranteed to be unique. They are often used as keys for object properties to avoid name collisions.
let sym1 = Symbol();let sym2 = Symbol("id"); // optional descriptionlet sym3 = Symbol("id");
console.log(sym2 === sym3); // false – each Symbol is uniqueUsing as property keys:
const obj = {};const id = Symbol("id");obj[id] = "secret value";console.log(obj[id]); // 'secret value'console.log(Object.keys(obj)); // [] – symbols are not enumeratedWell‑known symbols like Symbol.iterator are used to define built‑in behaviors.
const arr = [1, 2, 3];const iterator = arr[Symbol.iterator](); // returns an iteratorSymbols are not fully private but offer a way to create non‑string property keys that won’t clash with other keys.