Set.prototype.add() - JavaScript | MDN

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

Parameters

value

The value to add to the Set object. Objects are compared by reference, not by value.

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

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on by MDN contributors.