Set() constructor

The Set() constructor creates Set objects.

Try it


new Set()
new Set(iterable)

Note: Set() can only be constructed with new. Attempting to call it without new throws a TypeError.


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.


Using the Set object

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 };


ECMAScript Language Specification
# sec-set-constructor

Browser compatibility

BCD tables only load in the browser

See also