W3cubDocs

/JavaScript

Set.set

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

Syntax

new Set([iterable])

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

let 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' ]
let o = {a: 1, b: 2}
mySet.add(o)

Specifications

Browser compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
Set() constructor 38 12 13 11 25 8
new Set(iterable) 38 12 13 No 25 9
Set() without new throws 38 12 42 11 25 9
new Set(null) 38 12 37 11 25 9
Mobile
Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
Set() constructor 38 38 14 25 8 3.0
new Set(iterable) 38 38 14 25 9 3.0
Set() without new throws 38 38 42 25 9 3.0
new Set(null) 38 38 37 25 9 3.0
Server
Node.js
Set() constructor 0.12
0.12
0.10
Disabled
Disabled From version 0.10: this feature is behind the --harmony runtime flag.
new Set(iterable) 0.12
Set() without new throws 0.12
new Set(null) 0.12
0.12
0.10
Disabled
Disabled From version 0.10: this feature is behind the --harmony runtime flag.

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/Set