New in JS2015.
Set is the value of the property key
"Set" of the Global Object
console.log( window.Set === Set );
Set is a function.
console.log( typeof Set === "function" );
Set is Function.prototype.
console.log( Reflect.getPrototypeOf ( Set ) === Function.prototype );
- To create instance of “set” objects.
- Used as a namespace to hold general purpose methods for working with “set” objects.
- Holds the property key
"prototype". The value of
Set.prototypeis the parent object of all “set” instances.
“Set” objects are collections of values.
- Value can be any type.
- The values in a set are distinct. (a specific value can occur only once in a set.)
- The insertion order of values are maintained.
How Set Determines Uniqueness
The equality test used for determining whether 2 values in a set is the same as
except treatment of
NaN (not a number).
NaN === NaN return
false, but for set object,
NaN is considered same as any
// equality test used for determining whether 2 values in the set is the same as ===, except treatment of NaN // NaN === NaN is false console.log((NaN === NaN) === false); // but for set object, NaN is same as any NaN const ss = new Set([NaN, NaN]); console.log(ss.size === 1);