Learn how you can use JavaScript symbols to define unique property values as well as keys for identifying object properties.

Before symbols were introduced in ES6 as a new type of primitive, JavaScript used seven main types of data, grouped into two categories:

  1. Primitives, including the string, number, bigint, boolean, null, and undefined data types
  2. Objects, including more complex data structures, such as arrays, functions, and regular JS objects

Starting with ES6, symbols were added to the primitives group. Like all other primitives, they are immutable and have no methods of their own.

The original purpose of symbols was to provide globally unique values that were kept private and for internal use only. However, in the final implementation of this primitive type, symbols ended up not being private, but they did keep their value uniqueness.

We’ll address the privacy issue a bit later. As for the uniqueness of symbols, if you create two different symbols using the factory function Symbol(), their values will not be equal.

#javascript #web-development #es6

Understanding symbols in JavaScript
5.45 GEEK