The Set
constructor lets you create Set
objects that store unique values of any type, whether primitive values or object references.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
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 newSet
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
Specification |
---|
ECMAScript (ECMA-262) The definition of 'Set constructor' in that specification. |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Desktop | Mobile | Server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Set() constructor | Chrome Full support 38 | Edge Full support 12 | Firefox Full support 13 | IE Full support 11 | Opera Full support 25 | Safari Full support 8 | WebView Android Full support 38 | Chrome Android Full support 38 | Firefox Android Full support 14 | Opera Android Full support 25 | Safari iOS Full support 8 | Samsung Internet Android Full support 3.0 | nodejs
Full support
0.12
|
new Set(iterable) | Chrome Full support 38 | Edge Full support 12 | Firefox Full support 13 | IE No support No | Opera Full support 25 | Safari Full support 9 | WebView Android Full support 38 | Chrome Android Full support 38 | Firefox Android Full support 14 | Opera Android Full support 25 | Safari iOS Full support 9 | Samsung Internet Android Full support 3.0 | nodejs Full support 0.12 |
Set() without new throws | Chrome Full support 38 | Edge Full support 12 | Firefox Full support 42 | IE Full support 11 | Opera Full support 25 | Safari Full support 9 | WebView Android Full support 38 | Chrome Android Full support 38 | Firefox Android Full support 42 | Opera Android Full support 25 | Safari iOS Full support 9 | Samsung Internet Android Full support 3.0 | nodejs Full support 0.12 |
new Set(null) | Chrome Full support 38 | Edge Full support 12 | Firefox Full support 37 | IE Full support 11 | Opera Full support 25 | Safari Full support 9 | WebView Android Full support 38 | Chrome Android Full support 38 | Firefox Android Full support 37 | Opera Android Full support 25 | Safari iOS Full support 9 | Samsung Internet Android Full support 3.0 | nodejs
Full support
0.12
|
Legend
- Full support
- Full support
- No support
- No support
- User must explicitly enable this feature.
- User must explicitly enable this feature.