JavaScript Symbol Type gives a way to make unique values. A Symbol never matches any other value and this helps in many cases.
Table of Content
What is JavaScript Symbol Type
A Symbol in JavaScript is a new primitive type. It makes unique values that never match with other values in memory.
Here is an example:
let sym = Symbol("id");A Symbol can take a description as text. That text only helps for debugging and does not affect the value.
Use Symbols as object keys
Symbols help in making hidden keys in objects. They also help to define parts that stay safe from name clashes.
For example:
let id = Symbol("id");
let user = { name: "Ali" };
user[id] = 100;
console.log(user[id]);The Symbol makes a safe key that never collides with other keys in the object.
You can use a Symbol as a property key in any object. The key does not appear in normal loops but still holds the value.
Here is an example:
let sym = Symbol("secret");
let obj = { name: "Sara" };
obj[sym] = "hidden";
console.log(obj[sym]);The Symbol key holds the word “hidden”. A normal loop skips it but you can still get it by the Symbol.
For another example:
let sym = Symbol("data");
let obj = {};
Object.defineProperty(obj, sym, { value: 42 });
console.log(obj[sym]);The Symbol works as a safe property key. It avoids clash with any normal string key inside the object.
Global Symbol Registry with Symbol.for()
JavaScript also has a global Symbol registry. Symbol.for() searches that registry and returns the same Symbol for the same key.
For example:
let sym1 = Symbol.for("shared");
let sym2 = Symbol.for("shared");
console.log(sym1 === sym2);This returns true. Both variables hold the same Symbol because they came from the global registry.
Examples
Symbol as unique key:
let user = {};
let id = Symbol("id");
user[id] = 123;
console.log(user[id]);Here the Symbol id makes a unique key for the object. No other property can replace or break this value because the key is unique.
Symbol.for in registry:
let sym1 = Symbol.for("app");
let sym2 = Symbol.for("app");
console.log(sym1 === sym2);Both lines return the same Symbol because they come from the global registry. This helps in cases where you need shared identity.
Hide property with Symbol:
let sym = Symbol("secret");
let obj = { name: "Omar", age: 22 };
obj[sym] = "token";
for (let key in obj) console.log(key);The loop prints only name and age. The Symbol key does not appear, but you can still read it with the Symbol.
Symbol with defineProperty:
let sym = Symbol("meta");
let obj = {};
Object.defineProperty(obj, sym, { value: "data", enumerable: false });
console.log(Object.keys(obj));The Symbol key stays hidden from normal property checks. This helps to store data that must not mix with user keys.
Wrapping Up
In this article you learned what a Symbol is and how it works with objects. Here is a quick recap:
Symbols make unique values, they act as safe object keys, and they also exist in the global registry.
FAQs
What is JavaScript Symbol Type?
- JavaScript Symbol Type is a unique and immutable primitive.
- It is often used as object keys to avoid property name conflicts.
let sym = Symbol("id");
console.log(typeof sym); // "symbol"
How to create a Symbol in JavaScript?
- Use the
Symbol()function. - You can add a description for debugging.
let sym1 = Symbol();
let sym2 = Symbol("user");
console.log(sym1, sym2);
What are Symbols used for in JavaScript?
- Symbols can act as object keys.
- They help avoid accidental key overwrites.
let user = {};
let id = Symbol("id");
user[id] = 123;
console.log(user[id]); // 123
What is the difference between Symbol() and Symbol.for()?
Symbol()creates a new unique symbol each time.Symbol.for()checks the global registry and reuses if exists.
let sym1 = Symbol("key");
let sym2 = Symbol("key");
console.log(sym1 === sym2); // false
let g1 = Symbol.for("key");
let g2 = Symbol.for("key");
console.log(g1 === g2); // true
Similar Reads
In this guide, you will learn how JavaScript functions work and why they matter. Each section shows what you need…
Some numbers do not stay whole. You get decimal parts when you divide or calculate prices. You may need to…
You will learn what the JavaScript valueOf function does and how it works. It returns the primitive value of an…
Code mistakes used to slip by without a warning. That made bugs hard to trace and fix. JavaScript "use strict"…
Unary operators in JavaScript work with only one value. They can change, test, or change the type of that value,…
The JavaScript while loop runs code as long as a condition stays true. You can use it to repeat tasks…
JavaScript has a statement called "with" that changes how code accesses object properties. The"with" Function makes a temporary scope, so…
JavaScript includes Math.sign to find out if a number is positive. It also helps detect negative values or zero. It…
JavaScript gives you several ways to round numbers, but not all of them round the same way. If you want…
JavaScript optional chaining lets you access object values without runtime errors. It checks if a property exists before access and…