Javascript Symbol : a usefull datatype.

IIn this blog post, we will be discussing the JavaScript symbol

What is a JavaScript Symbol  ?

Goddess Isis figurine wallpaper

A symbol in JavaScript is a unique and immutable data type that is often used to identify object properties.
It is part of the new datatypes thaht JavaScript offers to us.

This helps to differentiate multiple properties of an object that are similar but different.
But what the hell are you saying ? Are you mad ?

You’ll undestand better the principe with an example later in this post.

How to create a Javascript Symbol ?

the big bang theory dvd

Creating a Javascript symbol is easy. You can use the Symbol() constructor to create a new symbol. To get the address of the symbol, use the AddressOf() function.

const guitare1 = Symbol('Vigier Excalibur blue ocean');
console.log(car1); //  Symbol(Vigier Excalibur blue ocean);

Create a symbol in car1 and store the description “Vigier Excalibur blue ocean” as its value. This will create a unique symbol that can only be accessed by using the description “Vigier Excalibur blue ocean.”

And now lt’s create a new Symbol:

const guitare2 = Symbol('Vigier Excalibur blue ocean');

console.log(guitare1 === guitare2); // false

The result is always false because the description is only used to describe the symbol. It’s not used as part of the symbol itself- each time a new symbol is created, regardless of its description.

How to Use the JavaScript Symbol ?

So let’s go and try JavaScript Symbol in a “real life” example.
Guitar collections can be represented using avaScript Symbol. Here’s the code to do so for the example.

assorted-color guitar lot

So here is my guitar collection.

const myGuitareCollection = {
'Vigier Excalibur 1992 Ocean blue': { lastChangeOfString: '02/2022', lastRevision: '10/2020', stringGauge: '10/42' },
'Fender Statocaster 1972 brown': { lastChangeOfString: '09/2021', lastRevision: '03/2021', stringGauge: '10/42' },
'Gibson LP Balck Beauty 2007': { lastChangeOfString: '02/2021', lastRevision: '10/2019', stringGauge: '10/42' },      'Vigier Excalibur 1992 Ocean blue': { lastChangeOfString: '01/2022', lastRevision: '01/2022', stringGauge: '11/46' },
};

As you can see i own twin guitars Vigier Excalibur 1992 Ocean blue.
But there is a problem with the Object myGuitareCollection above.

If i console log the object it will print:

Object {Vigier Excalibur 1992 Ocean blue: Object, Fender Statocaster 1972 brown: Object, Gibson LP Balck Beauty 2007: Object}

Instead of adding another Vigier Excalibur 1992 Ocean blue to the collection, our previous Vigier Excalibur 1992 Ocean blue is overwritten by the new Vigier Excalibur 1992 Ocean blue being added to the collection. To fix this problem, we can use JavaScript Symbol. Let rewrite my collection with JavaScript Symbol:

const myGuitareCollection = {
[Symbol('Vigier Excalibur 1992 Ocean blue')]: { lastChangeOfString: '02/2022', lastRevision: '10/2020', stringGauge: '10/42' },
[Symbol('Fender Statocaster 1972 brown')]: { lastChangeOfString: '09/2021', lastRevision: '03/2021', stringGauge: '10/42' },
[Symbol('Gibson LP Balck Beauty 2007')]: { lastChangeOfString: '02/2021', lastRevision: '10/2019', stringGauge: '10/42' },     [Symbol('Vigier Excalibur 1992 Ocean blue')]': { lastChangeOfString: '01/2022', lastRevision: '01/2022', stringGauge: '11/46' },
};

Now If i console log the object it will print:

Object {Symbol(Vigier Excalibur 1992 Ocean blue): Object, Symbol(Fender Statocaster 1972 brown): Object, Symbol(Gibson LP Balck Beauty 2007): Object, Symbol(Vigier Excalibur 1992 Ocean blue): Object}

By changing the myGuitareCollection’s properties to use javascript symbol, each property is a unique JavaScript Symbol and the first javascript symbol Symbol(Vigier Excalibur 1992 Ocean blue) doesn’t get overwritten by the second Symbol(Vigier Excalibur 1992 Ocean blue).

Conclusion:

Symbols are a great way to identify variables and constants. They can be used in many different ways, depending on the context. For example, you could use a symbol to identify a variable that stores an integer value, or you could use a symbol to identify a constant that represents the length of time in seconds. Symbols can also be used to represent other types of data, such as strings or dates.

Leave A Comment