[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Set/add [Back]  [Original]

Set.prototype.add() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

Set.prototype.add()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 20157.

add() Set value

In this article

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
}

js
mySet.add(value);

value

Set

Set

add

js
var mySet = new Set();

mySet.add(1);
mySet.add(5).add("some text"); // chainable

console.log(mySet);
// Set [1, 5, "some text"]

Specification
ECMAScript 2027 LanguageSpecification
# sec-set.prototype.add


Web Proxy Viewer  |  New URL  |  Original Page