4

I have been just introduced to the concept of Symbols in JavaScript. I have been informed that they can be used to create unique identifiers to avoid potential clashes.

For example...

let user = {
    name        : "John",
    Symbol("id"): 123,    // Symbol("id") is a unique identifier
    Symbol("id"): 123     // and the Symbol("id") here is also different and unique
 };

... I understand the above code. However, what is the actual identifier value of each "Symbol("id")"? How do I find out?

Any pointers appreciated.

Grateful
  • 9,685
  • 10
  • 45
  • 77
  • I don't understand the question. What do you mean by the value of the symbol? A symbol is just an object that has a name, and can be used as a unique identifier. – Barmar Oct 21 '19 at 05:08
  • Are you asking how to go from `id1` to the string `"id"`? – Barmar Oct 21 '19 at 05:09
  • @Barmar I am simply asking about the method that allows us to see the unique identifier contents. At the moment, I have been told that the identifiers are unique... But how can I actually view the contents to confirm? – Grateful Oct 21 '19 at 05:15
  • `console.log(id1 == id2)` will say `false`, so you know they're different even though they both contain `id`. – Barmar Oct 21 '19 at 05:17
  • @Barmar Yes, thank you very much... I know about that already. However, I still want to be able to see the contents if possible... Is it possible? – Grateful Oct 21 '19 at 05:24
  • You mean the internal identifier *maybe* used by the browser? Do you want to find this through web APIs or though some kind of debugger? The former is probably impossible, the latter... might be possible through a memory-dump? Will highly depend on your browser. – Kaiido Oct 21 '19 at 05:24
  • 1
    @Grateful You're thinking about it the wrong way, the Symbol itself is a unique identifier. There is no "content" or "value" that you can extract from it. – Matt Aft Oct 21 '19 at 05:25
  • @Kaiido Oh really!? So there is no way of finding out about the actual contents of Symbol("id") through javascript? – Grateful Oct 21 '19 at 05:25
  • There is no "content", it's just a Symbol by itself. Just like a number is just a number and two numbers even if they have the same value will be two different objects for the memory. – Kaiido Oct 21 '19 at 05:27
  • They're unique because they happen to be in different memory locations, just like two arrays that happen to have the same contents. – Barmar Oct 21 '19 at 05:30

4 Answers4

2

const id1 = Symbol("id");
const id2 = Symbol("id");

const user = {
    name: "John",
    [id1]: 123,    // "[id1]" is a unique identifier
    [id2]: 456,    // and the value of "[id2]" here is also different
};

console.log('id1:', user[id1], id1.description);
console.log('id2:', user[id2], id2.description);
gman
  • 100,619
  • 31
  • 269
  • 393
  • Thank you for trying to help me. To be specific though, I am not talking about the values "123" or "456". I am talking about the actual identifier values themselves. – Grateful Oct 21 '19 at 05:18
1

No, you cannot see the "raw" value of the symbol in the JS environment, because it is implemented using the native C ++ code of the JS engine itself, and this implementation does not provide an opportunity to display it anywhere in the console or on a page.

You can think of symbols as big numbers and every time you create a symbol, a new random number gets generated (uuid). You can use that symbol (the random big number) as a key in objects. Regarding this definition, you can look at a possible implementation of the symbol in ES5:

var Symbol;
if (!Symbol) {
Symbol = (function(Object){

// (C) WebReflection Mit Style License

var ObjectPrototype = Object.prototype,
    defineProperty = Object.defineProperty,
    prefix = '__simbol' + Math.random() + '__',
    id = 0;

//... some other code

You can see that Math.random() is used here, so Symbol there will have long big number as a his main property (unique with high possibility).

Another way to explain what a symbol is is that a Symbol is just a piece of memory in which you can store some data. Each symbol will point to a different memory location. In the context of this definition, you can see the source code in C++ of the JS engine itself, for example V8 that is used in Chromium. If you know C++, you can try to find an implementation of Symbol() constructor there, but it won't be easy.

Therefore, we can say that a Symbol is a kind of unique memory area that a certain string describes. The memory area itself it's already term from a low-level programming, so you can think of it as something like 1010010011...

Xth
  • 710
  • 1
  • 5
  • 11
0

I wasn't able to get your question properly, I tried to help you hope this will work

let user = { // belongs to another code
  name: "Alex"
};

let id = Symbol("id");

user[id] = 200;

alert( user[id] ); // we can access the data using the symbol as the key
0

From mdn:

Every symbol value returned from Symbol() is unique. A symbol value may be used as an identifier for object properties; this is the data type's only purpose.

 console.log(Symbol('foo') === Symbol('foo'))
 

From an article in this answer:

ES6 symbols are similar to the more traditional symbols in languages like Lisp and Ruby, but not so closely integrated into the language. In Lisp, all identifiers are symbols. In JS, identifiers and most property keys are still considered strings. Symbols are just an extra option.

As mdn docs explains, you could access the Description that you passed but not the value of the Symbol:

Most values in JavaScript support implicit conversion to a string. For instance, we can alert almost any value, and it will work. Symbols are special. They don’t auto-convert.

For example,

let Sym = Symbol("Sym");
alert(Sym); // TypeError: Cannot convert a Symbol value to a string

That’s a "language guard" against messing up, because strings and symbols are fundamentally different and should not occasionally convert one into another.

If we really want to show a symbol, we need to call .toString() on it, for example,

 
let Sym = Symbol("Sym");
alert(Sym.toString()); // Symbol(Sym), now it works 

Or we can use get symbol.description property to get the description on it, for example,

 
let _Sym = Symbol("Sym");
alert(_Sym.description); // Sym 
 
gman
  • 100,619
  • 31
  • 269
  • 393
shrys
  • 5,860
  • 2
  • 21
  • 36