Baseline Widely available
The Set()
constructor creates Set
objects.
const set1 = new Set([1, 2, 3, 4, 5]);
console.log(set1.has(1));
// Expected output: true
console.log(set1.has(5));
// Expected output: true
console.log(set1.has(6));
// Expected output: false
Syntax
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.
A new Set
object.
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 See also
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4