Say I create a Symbol:
Symbol('r2g.smoke.test')
Isn't that the same as String('@@r2g.smoke.test')
, or no?
Say I create a Symbol:
Symbol('r2g.smoke.test')
Isn't that the same as String('@@r2g.smoke.test')
, or no?
No, a Symbol with a description of r2g.smoke.test
is not related in any way to the string @@r2g.smoke.test
. Symbol's in general are nothing like strings.
No, while printing a Symbol may add @
signs for human readability, an ES2015 symbol is very different than a string.
The key thing about Symbol
is that every time you call it, you get a unique version of the symbol. The string that you pass in (e.g. 'r2g.smoke.test'
) is just a helpful description.
Some code to demonstrate:
~$ node
> const string1 = "hello"
> const string2 = "hello"
> string1 === string2
true
> const symbol1 = Symbol("hello")
> const symbol2 = Symbol("hello")
> symbol1 === symbol2
false
It is guaranteed that every time you call Symbol(), you will get a unique symbol that is different from every other symbol, EVEN if the description you pass in is the same