javascript instance method
Set.prototype.add()
The add() method of Set instances inserts the specified value into this set, if it is not already present.
Interactive exampleOpen the canonical MDN source to run this embedded demo.
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
add(value)
Parameters
value- : The value to add to the
Setobject. Objects are compared by reference, not by value.
- : The value to add to the
Return value
The Set object.
Examples
Using add()
const mySet = new Set();
mySet.add(1);
mySet.add(5).add("some text"); // chainable
console.log(mySet);
// Set [1, 5, "some text"]
Specifications
SpecificationsStandards references are available on the canonical MDN page.
Browser compatibility
Browser compatibilityCompatibility data is available on the canonical MDN page.