W3cubDocs

/JavaScript

Set() constructor

The Set lets you create Set objects that store unique values of any type, whether primitive values or object references.

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 Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet Deno Node.js
Set
38
12
13
11
25
8
38
38
14
25
8
3.0
1.0
0.12.0
0.10.0
iterable_allowed
38
12
13
No
25
9
38
38
14
25
9
3.0
1.0
0.12.0
new_required
38
12
42
11
25
9
38
38
42
25
9
3.0
1.0
0.12.0
null_allowed
38
12
37
11
25
9
38
38
37
25
9
3.0
1.0
0.12.0
0.10.0

See also

© 2005–2022 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