Set.prototype.add()

Adds a value to a Set if it is not already present.

Since ES2015 (ES6) Spec ↗

Syntax

set.add(value)

Returns

Set — The `Set` itself, allowing chained calls.

Examples

const s = new Set();
s.add(1).add(2).add(2);
console.log([...s]);
Output
[ 1, 2 ]
const s = new Set();
s.add(NaN);
s.add(NaN);
console.log(s.size);
Output
1

Notes

- Duplicate values are ignored; uniqueness uses SameValueZero so `NaN` is treated as equal to `NaN`. - Returns the set, enabling chained `add()` calls.

Browser & runtime support

EnvironmentSince version
chrome 38
firefox 13
safari 8
edge 12
node 0.12

See also