JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

Follow publication

Member-only story

Understanding Symbols in JavaScript: A Guide to Unique and Powerful Object Keys

Unique Keys and Advanced Object Customization

✨ Satish Kumar
JavaScript in Plain English
4 min readNov 22, 2024

In JavaScript, symbols are a unique and immutable primitive data type introduced in ES6 (ECMAScript 2015).

Symbols are often used to add unique property keys to an object that won’t collide with keys any other code might add to the object, and which are hidden from any mechanisms other code will typically use to access the object.

Dont have an active subscription read it here

Creating a Symbol

A Symbol is created using the Symbol() function:

const sym1 = Symbol();
const sym2 = Symbol("desc");
const sym3 = Symbol("desc");

Key Features of Symbols

  1. Uniqueness: Every Symbol is guaranteed to be unique.
const sym1 = Symbol("desc");
const sym2 = Symbol("desc");

console.log(sym1 === sym2); // false

Even if two symbols have the same description, they are different.

Note that Symbol("desc") does not coerce the string "desc" into a Symbol. It creates a new Symbol each time

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Published in JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

Written by ✨ Satish Kumar

AVP Engineering - frontend Apps at fleetx.io Ex-Aviso Inc. NITian 🎓

No responses yet

Write a response