0

I Googled a lot about new primitive type "Symbol" but still cannot understand actual use of it. What I understood is:

  • They are a primitive type, just as integer or string.
  • They are not a constructor function, hence you cannot use new Symbol() syntax.

But why is it used? String/Integer/Boolean have their uses which are clear, but what's the use of Symbol() exactly?

Moreover, for this code:

var data1 = Symbol();
var data2= Symbol('dummy');

why Symbol('dummy') === Symbol('dummy') is false?

Code_Tech
  • 775
  • 13
  • 42
  • 1
    Everything is explained in the [docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol) tho – Alexandre Elshobokshy Jan 03 '19 at 08:51
  • the hand over value is only a description, not an identifier. this is the returned symbol itself. – Nina Scholz Jan 03 '19 at 08:51
  • Possible duplicate of [What is the motivation for bringing Symbols to ES6?](https://stackoverflow.com/questions/21724326) and [What are the possible usage scenarios for the new JavaScript “Symbol” datatype?](https://stackoverflow.com/questions/27172762) and [What is the “symbol” primitive data type in JavaScript](https://stackoverflow.com/questions/36797206) – adiga Jan 03 '19 at 08:54

1 Answers1

0

This code Symbol('dummy') === Symbol('dummy') is false because each symbol is strictly unique.

Even if you use the same description (dummy), they are actually two different unique symbols.

They are used as identifiers.

You can read more about them here

quirimmo
  • 9,800
  • 3
  • 30
  • 45