Set() constructor
Baseline Widely availableThis 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
const set = new Set([1, 2, 3, 4, 5]);
console.log(set.has(1));
// Expected output: true
console.log(set.has(5));
// Expected output: true
console.log(set.has(6));
// Expected output: false
Syntax
jsnew Set()
new Set(iterable)
Note:
Set() can only be constructed with new. Attempting to call it without new throws a TypeError.
Parameters
iterableOptional-
If an iterable 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 new Set object.
Examples
>Using the Set object
jsconst 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® 2027 Language Specification > # sec-set-constructor > |
Browser compatibility
See also
Help improve MDN
Was this page helpful to you? Yes No Learn how to contributeThis page was last modified on Oct 26, 2026 by MDN contributors.
View this page on GitHub • Report a problem with this content