Set() constructor
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The Set() constructor creates Set objects.
Try it
Syntax
Parameters
iterableOptional-
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 newSetis empty.
Return value
A new Set object.
Examples
Using the Set object
js
const 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' ]
const o = { a: 1, b: 2 };
mySet.add(o);
Specifications
| Specification |
|---|
| ECMAScript Language Specification # sec-set-constructor |
Browser compatibility
| desktop | mobile | server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Set() constructor | |||||||||||||
new Set(iterable) | |||||||||||||
new Set(null) | |||||||||||||