JavaScript: Set Object

By Xah Lee. Date: . Last updated: .

New in ES2015.

See also: JavaScript: 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 JavaScript: 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 JavaScript: 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 JavaScript: Iterable

ECMAScript 2015 §Keyed Collection#sec-set-constructor

Properties


Set.prototype

JavaScript: Set.prototype

Reference

ECMAScript 2015 §Keyed Collection#sec-set-objects

Set Topic

  1. JavaScript: the Set Object Tutorial
  2. JavaScript: Set Union, Intersection, Difference
  3. JavaScript: Set Object
  4. JavaScript: Set.prototype
  5. JavaScript: Set.prototype.forEach
Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.