Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Understanding the Symbol
2.1.
Using Symbols as Object Keys
2.2.
The Symbol.for and Symbol.keyFor Methods
3.
Understanding Well-Known Symbols
4.
Frequently Asked Questions
4.1.
What is a Symbol in JavaScript?
4.2.
How do you create a Symbol?
4.3.
What is the use of Symbol.for and Symbol.keyFor methods?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

JavaScript Symbol

Author Gunjan Batra
0 upvote
Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM

Introduction

As JavaScript continues to evolve, it has been adding features that extend its capabilities. One such addition is the Symbol, a unique, immutable data type introduced in ECMAScript 2015. 

JavaScript Symbol

In this article, we will delve into the concept of the Symbol, including its creation, use cases, properties, and methods.

Understanding the Symbol

In JavaScript, a Symbol is a unique and immutable data type. When you create a Symbol, JavaScript ensures it's unique, even if two Symbols have the same description.

To create a Symbol, you use the Symbol() function. This function can accept an optional string parameter that represents the Symbol's description. However, this description doesn't impact the Symbol's uniqueness.


let symbol1 = Symbol();
let symbol2 = Symbol("symbol description");
let symbol3 = Symbol("symbol description");

Even though symbol2 and symbol3 have the same description, they are not the same.

Using Symbols as Object Keys

One of the main uses of Symbols is as keys in an object. Because every Symbol is unique, they allow us to create properties in objects that won't clash with any other property or method, thereby avoiding bugs that could arise from overwriting data.

let employee = {};
let id = Symbol('id');
employee[id] = 'E1001';

In the above code, the id Symbol is used as a property key in the employee object. The Symbol ensures the uniqueness of the property.

The Symbol.for and Symbol.keyFor Methods

JavaScript's Symbol object includes the Symbol.for(key) and Symbol.keyFor(sym) methods. The Symbol.for(key) method searches for existing Symbols with the given key and returns it if found. If not found, it creates a new Symbol with the given key.


let symbol1 = Symbol.for("employee");
let symbol2 = Symbol.for("employee");
console.log(symbol1 === symbol2);

/Outputs: 

True
Output

The Symbol.keyFor(sym) method returns a string representing the key for the Symbol sym in the global Symbol registry.

let symbol1 = Symbol.for("employee");
console.log(Symbol.keyFor(symbol1));   

Outputs:

"employee"
Output

 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Understanding Well-Known Symbols

JavaScript has some pre-defined Symbols known as well-known Symbols. These Symbols are used to represent internal language behaviors that were not previously exposed to developers. They include Symbol.iterator, Symbol.asyncIterator, Symbol.toStringTag, among others.

For instance, the Symbol.iterator is used by for...of loops to get the default iterator of an object.

let arr = [1, 2, 3];
let iterator = arr[Symbol.iterator]();
console.log(iterator.next());  

  Outputs:

Outputs

Frequently Asked Questions

What is a Symbol in JavaScript?

A Symbol is a unique, immutable data type in JavaScript used mainly as object keys to avoid name clashes.

How do you create a Symbol?

You can create a Symbol using the Symbol() function.

What is the use of Symbol.for and Symbol.keyFor methods?

Symbol.for(key) searches for or creates a Symbol with the given key. Symbol.keyFor(sym) returns the key of a Symbol in the global registry.

Conclusion

Symbols in JavaScript, although a bit abstract, provide a powerful way to handle unique keys in objects, well-defined interfaces, and hidden implementation details. Understanding and using Symbols can unlock a new level of coding efficiency, leading to cleaner, safer, and bug-resistant code. As JavaScript continues to evolve, the Symbol will undoubtedly play a significant role in shaping the language.

To learn more about JavaScript, we recommend reading the following articles:

If you liked our article, do upvote our article and help other ninjas grow.  You can refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more!

Happy Coding!
 

Previous article
Promise .then() Function in JavaScript
Next article
finally() Method in JavaScript
Live masterclass