Symbols in JavaScript
Tags: javascript, webdev, beginners, programming
Published: 2022-08-27 11:44:38

As mentioned in the previous article symbols in JavaScript are immutable and unique. But the follow up to that statement would be - " What do you mean  by immutable ? ". A variables is said to be immutable if its value cannot be changed. But in javascript we change the values of variables all the times so how come they are immutable ? 

When we declare a variable and assign it some value, what really happens behind the curtain is that a space in memory is created and in that space the value is stored. The variable simply points to this space in memory. However, when we reassign the value of a variable it does not changes the value stored in the memory space the variable is pointing at, it creates a new memory space and stores the new value in it and now starts pointing to this space.

## What are Symbols ?
Since now the concept of immutability is clear to you, lets focus our attention on a real life example. My friends in India are well aware of the AADHAR card they have. My american friends must be aware of social security number. AADHAR card number and social security numbers are unique and immutable i.e. no two of them are same and they cannot be changed. They are our digital fingerprints. 

Symbols are somewhat similar to these digital fingerprint, every variable of symbol type is unique and immutable.

> Symbols do not store any literal value i.e. they cannot store numbers, string, boolean values etc

## Creating variables of type symbols
Variables of type symbol can only be created by using the `Symbol()` function, `Symbol()` takes an optional argument which is called the description parameter. This parameter is often used to describe what the symbol was created for. Look at the code example below for better understanding.

```javascript
var x = Symbol(); // no optional argument passed
var y = Symbol("optionalArgument"); // optional argument i.e. description provided
console.log(typeof x);
console.log(typeof y);
```
```
symbol
symbol
```
Every time `Symbol()` is called it returns a unique instance of symbol object. If you rum the code below the output will always be false just like it was in my case

```javascript
console.log(Symbol() === Symbol());
```
```
false
```

## Why Symbols exists in the first place ?
If symbols don't hold any literal value then what is the purpose of this data type. The answer lies in the nature of this data type. As it was mentioned earlier that each call of the `Symbol()` function return a unique instance of symbol type. This property makes it very suitable to be used in any database where unique ids are required for each membr of the data base. Use of instances of `symbol` type makes it easier to programmatically generate these ids.

## Sharing of Symbols
If you have any experience with RDBMS, you must be aware of the concept of foreign keys and primary keys. Foreign keys are primary keys of members of other tables. But how do we share the ids in javascript using symbols.

If you wish to create symbols that need to be shared then they need to be created using the `Symbol.for()` method.  `Symbol.for()` method accepts key parameter. If there has already been a symbol with the same key then that instance is copied, however if no such key exists then an new symbol instance is created with the given key. 


```javascript
let id1 = Symbol.for("member1");
let id2 = Symbol.for("member1");
console.log(id1 === id2);
```

```
true
```