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

new Set()
new Set(iterable)

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

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

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

Browser compatibility

Desktop Mobile Server
Chrome Edge Firefox Opera Safari Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet WebView Android Deno Node.js
Set 38 12 13 25 8 38 14 25 8 3.0 38 1.0 0.12.0
iterable_allowed 38 12 13 25 9 38 14 25 9 3.0 38 1.0 0.12.0
null_allowed 38 12 37 25 9 38 37 25 9 3.0 38 1.0 0.12.0

See also

© 2005–2024 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/Set