-1

Say I create a Symbol:

Symbol('r2g.smoke.test')

Isn't that the same as String('@@r2g.smoke.test'), or no?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 7
    Short answer, no. What makes you think that? – Matus Dubrava May 07 '18 at 20:00
  • 1
    @MatusDubrava: [The spec uses `@@...` to refer to symbols](https://www.ecma-international.org/ecma-262/6.0/#sec-well-known-symbols) and these names are often used in polyfills if the environment doesn't support symbols. – Felix Kling May 07 '18 at 20:30
  • 1
    A symbol is its own primitive type, *along with* Booleans, Strings, Numbers, Null and Undefined. – Felix Kling May 07 '18 at 20:32
  • very related: [What does @@ (“at at”) mean in ES6 JavaScript?](https://stackoverflow.com/q/29492333/1048572) and [Symbol.iterator vs @@iterator](https://stackoverflow.com/q/29670320/1048572) – Bergi May 08 '18 at 20:08

2 Answers2

3

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.

Paul
  • 139,544
  • 27
  • 275
  • 264
  • right an object can take only a number or string as a key, so symbols must be able to be stringifiable, etc –  May 07 '18 at 20:52
  • 1
    @OlegzandrDenman Objects can only have strings or symbols as properties. They cannot have numbers. Anything that isn't a string or symbol is coerced to a string before being used as a key: `var obj = {}; obj[1] = 'test'; Object.keys( obj ).forEach( key => console.log( key, typeof key ) );` outputs `1 string`. Unlike numbers (and all other types), symbols are not converted to strings when used as object properties, since that would mostly defeat the purpose of them. – Paul May 07 '18 at 21:01
2

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

AnilRedshift
  • 7,937
  • 7
  • 35
  • 59
  • Actually, I think you might be wrong, `Symbol('x') === Symbol('x')` always, read the docs –  May 07 '18 at 20:53
  • 3
    @OlegzandrDenman He isn't wrong `Symbol('x') !== Symbol('x')`, always. That's in contrast to `Symbol.for('x') === Symbol.for('x')` which should be avoided when possible for the same reasons that global variables should be avoided. – Paul May 07 '18 at 20:59
  • @OlegzandrDenman Related: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/for – Paul May 07 '18 at 21:03