Grateful
Grateful

Reputation: 10175

Is it possible to access the value of a Symbol in JavaScript?

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.

Upvotes: 4

Views: 2362

Answers (4)

Xth
Xth

Reputation: 820

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...

Upvotes: 2

shrys
shrys

Reputation: 5940

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 
 

Upvotes: 0

user128511
user128511

Reputation:

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);

Upvotes: 2

Navdeep Paliwal
Navdeep Paliwal

Reputation: 11

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

Upvotes: 0

Related Questions