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.
TheSet() constructor createsSet objects.
In this article
Try it
const set = new Set([1, 2, 3, 4, 5]);console.log(set.has(1));// Expected output: trueconsole.log(set.has(5));// Expected output: trueconsole.log(set.has(6));// Expected output: falseSyntax
js
new Set()new Set(iterable)Parameters
iterableOptionalIf aniterable object (such as an array) is passed, all of its elements will be added to the new
Set. If you don't specify this parameter, or its value isnullorundefined, the newSetis empty.
Return value
A newSet object.
Examples
>Using theSet 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® 2026 Language Specification> # sec-set-constructor> |