<JGustavsson/>

Javascript symbol - a short explanation

July 05, 2020

Simply put a symbol represents a completely unique identifier. Symbol is a new primitive data type, just like Number, String and Boolean.

You create a symbol through the Symbol() function.

const symbol1 = Symbol();
const symbol2 = Symbol();

console.log(symbol1 === symbol2); // false

Since a symbol is always unique Symbol() === Symbol() is false.

Why is this useful?

With symbols we can create “hidden” object properties that can not be overwritten or accessed from anywhere else.

let user = {
  name: "Bob",
};

// Create our unique symbol
let id = Symbol("id"); // the string parameter is just a description

user[id] = 1;
console.log(user[id]); // 1

Because we have access to our id symbol here, only we can access the id property value. It cannot be reached by a third party, unless of course we somehow expose our symbol variable.

This way another script can also add its own “id” symbol. There will be no conflict because symbols are always unique.