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 a new element with a specified value in to this set, if there isn't an element with the same value already in this set

Try it

const set1 = new Set();

set1.add(42);
set1.add(42);
set1.add(13);

for (const item of set1) {
  console.log(item);
  // Expected output: 42
  // Expected output: 13
}

Syntax

js
add(value)

Parameters

value

The value of the element to add to the Set object.

Return value

The Set object with added value.

Examples

Using the add() method

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® 2025 Language Specification
# sec-set.prototype.add

Browser compatibility

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
add

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

See also