Sets in JavaScript

Sets are shallow.

Sets in JavaScript
Yes, [0, 1] and [0, 1] are distinct in the above example🤓

In JavaScript, there are two types of values. One is a primitive data type and another one is an object. Primitives in JavaScript are as follows:

  • number
  • string
  • bigint
  • boolean
  • undefined
  • symbol
  • null.

An array is not a primitive type but an object. In JavaScript, objects are always referenced. Keeping it in mind, please take a look at the illustration below.

There are two distinct instances of [0, 1] and their references. What we add to the set are the values in the stack area, i.e. the references to the arrays in this case.