Table of Content
JavaScript Symbol Type gives a way to make unique values. A Symbol never matches any other value and this helps in many cases.
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
Some numbers come from user input and other come from math operations. Sometimes, these numbers need to stay positive. The…
JavaScript Math.sqrt() solves the need to get square roots fast. Before it, you wrote custom code or used loops. It…
The JavaScript nullish coalescing operator (??) gives a default value when a variable is null or undefined. Understand the Nullish…
The for...of loop appeared to solve the problem of complex loops over arrays and strings. It gives you a way…
Math.atan() function in JavaScript helps you to solve math problems in your code. It turns a number into an angle.…
If you are a coder, one of your primary requirements is to have a trustworthy code- editor. A Code editor…
The assignment operator in JavaScript stores and updates values. It helps you hold data in a variable and change it…
Code mistakes used to slip by without a warning. That made bugs hard to trace and fix. JavaScript "use strict"…
JavaScript runs code in different ways, but the for loop stays one of the most common tools. You use it…
You have to organize your JavaScript code structure. A clean layout helps you read and fix your code. Whether you…