🔣 Symbol Data Type
Last Updated: 08 Oct 2025
- Symbol is a primitive data type introduced in ES6 (ECMAScript 2015).
- It is used to create unique identifiers — even if two symbols have the same description, they are always different.
- Symbols are mainly used as object property keys to avoid accidental overwriting
Hinglish Tip 🗣: Symbol ka matlab hai unique value — agar do Symbol same naam se bhi banao, fir bhi dono alag honge. Ye ID card jaisa hai — naam same ho sakta hai, par number unique hota hai
let symbol1 = Symbol("id");
let symbol2 = Symbol("id");
✏ Creating a Symbol
Use the Symbol() function to create a new symbol.
let sym = Symbol("mySymbol");
console.log(sym); // Symbol(mySymbol)
console.log(typeof sym); // "symbol"
- The string "mySymbol" is just a description (for debugging).
- Each call to Symbol() creates a completely new unique symbol.
🧠 Global Symbols (Symbol.for)
If you want a shared symbol that can be reused, use Symbol.for(). It checks the global symbol registry.
let sym1 = Symbol.for("mySymbol");
let sym2 = Symbol.for("mySymbol");
Hinglish Tip 🗣: Symbol.for() ek global list me symbol store karta hai — agar same naam se dubara banao, to wohi symbol milta hai.