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 2015年7月.
add()
は Set
インスタンスのメソッドで、この集合に同じ値を持つ要素がない場合、指定した値を持つ新しい要素をこの集合に追加します。
試してみましょう
const set = new Set();
set.add(42);
set.add(42);
set.add(13);
for (const item of set) {
console.log(item);
// 予想される結果: 42
// 予想される結果: 13
}
構文
js
add(value)
引数
value
-
Set
オブジェクトに追加する要素の値です。
返値
追加された値を持つ Set
オブジェクトです。
例
>add() メソッドの使用
js
const mySet = new Set();
mySet.add(1);
mySet.add(5).add("some text"); // 連鎖可能
console.log(mySet);
// Set [1, 5, "some text"]
仕様書
Specification |
---|
ECMAScript® 2026 Language Specification> # sec-set.prototype.add> |
ブラウザーの互換性
Loading…