Set.prototype.add()
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 add() method of Set instances inserts the specified value into this set, if it is not already present.
Try it
const set = new Set();
set.add(42);
set.add(42);
set.add(13);
for (const item of set) {
console.log(item);
// Expected output: 42
// Expected output: 13
}
Syntax
js
add(value)
Parameters
Return value
The Set object.
Examples
>Using add()
js
const mySet = new Set();
mySet.add(1);
mySet.add(5).add("some text"); // chainable
console.log(mySet);
// Set [1, 5, "some text"]
Specifications
| Specification |
|---|
| ECMAScriptĀ® 2026 LanguageĀ Specification> # sec-set.prototype.add> |