Set() constructor

The Set constructor lets you create Set objects that store unique values of any type, whether primitive values or object references.

Syntax

new Set([iterable])

Parameters

iterable Optional

If an iterable object is passed, all of its elements will be added to the new Set.

If you don't specify this parameter, or its value is null, the new Set is empty.

Return value

A new Set object.

Examples

Using the Set object

let mySet = new Set()

mySet.add(1)           // Set [ 1 ]
mySet.add(5)           // Set [ 1, 5 ]
mySet.add(5)           // Set [ 1, 5 ]
mySet.add('some text') // Set [ 1, 5, 'some text' ]
let o = {a: 1, b: 2}
mySet.add(o)

Specifications

Specification
ECMAScript (ECMA-262)
The definition of 'Set constructor' in that specification.

Browser compatibility

BCD tables only load in the browser

See also