JS: Set Object

By Xah Lee. Date: . Last updated: .

New in ES2015.

See also: JS: the Set Object Tutorial

Set is the value of the property key "Set" of the global object.

console.log( window["Set"] === Set ); // true

Set is a function.

〔►see JS: Data Types

// type of Set
console.log ( typeof Set === "function" ); // true
console.log ( Object.prototype.toString.call(Set) === "[object Function]" ) // true

The parent of Set is Function.prototype. 〔►see JS: Prototype and Inheritance

// parent of Set
console.log ( Object.getPrototypeOf ( Set ) === Function.prototype ); // true

The purpose of Set is:

  1. To create instance of “set” objects.
  2. Used as a namespace to hold general purpose methods for working with “set” objects.
  3. Holds the property key "prototype". The value of Set.prototype is the parent object of all “set” instances.

“Set” objects are collections of values of any type. A distinct value may only occur once as value of a Set's collection. The insertion order of values are maintained.

Set Constructor

new Set(iterable) → create a set from values in iterable object iterable.

// create a set, add value to it
var s = new Set();
s.add ("6");
s.add ("5");
s.add ("6");
console.log(s) // Set { '5', '6' }

Any iterable object can be used as argument. 〔►see JS: Iterable

ECMAScript 2015 §Keyed Collection#sec-set-constructor



JS: Set.prototype


ECMAScript 2015 §Keyed Collection#sec-set-objects

Set Topic

  1. JS: the Set Object Tutorial
  2. JS: Set Union, Intersection, Difference
  3. JS: Set Object
  4. JS: Set.prototype
  5. JS: Set.prototype.forEach
Like what you read? Buy JavaScript in Depth