Member-only story
What are Symbols in javascript?
Do not have Medium Premium; read it for free here: SIMPLIFIED.NINJA
Objects in Javascript are a widely used datatype for quick data storage and retrieval. The problem with objects is that there is no way to make a few properties private or exclude them from the iteration unless explicitly requested for those properties.
For example, you have a user object and want to print all the details inside the object except the password. The only way to exclude is to add an explicit condition to your loop while iterating over the object. Just imagine how messy your code will be if you do the same for multiple properties.
Luckily, Javascript introduced a new type, “Symbols,” in ES6, which will solve our problem.
Let’s dive deep into:
- What is a Symbol?
- What are the use cases of Symbols?
- How to use Symbols to resolve our problem with objects?
- Finally, Some tips on how to use symbols to code more efficiently.
Introduction
Symbols are a new primitive datatype introduced in Javascript. Symbols represent unique and immutable identifiers despite having the same name/description. This uniqueness made Symbols the best choice for keys or identifiers in Objects.