Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (13.1k points)
Can anyone help me why we require a symbol primitive type and what it is used for?

1 Answer

0 votes
by (26.7k points)

Basically, it comes in use for private and unique keys. No one can able to set the specific map if he/she doesn't share the instance as the property of the symbol. You can take the below code as a reference:

var map = {};

var symbol1 = Symbol("prop");

var symbol2 = Symbol("prop"); // same name, different instance – so it's a different symbol!

map[symbol1] = 1;

map[symbol2] = 2; // doesn't override the previous symbol's value

console.log(map[symbol1] + map[symbol2]); // logs 3

I hope this will help.

Want to know more about Java? Prefer this tutorial on Learn Java.

Want to become a Java Expert? Join Java Training now!!

Related questions

0 votes
1 answer
asked Mar 31, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer
asked Apr 8, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer
asked Sep 3, 2020 in Python by ashely (50.2k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...