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.

Phương thức add() của các instance Set chèn giá trị được chỉ định vào set này, nếu giá trị đó chưa có mặt.

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
}

Cú pháp

js
add(value)

Tham số

value

Giá trị cần thêm vào đối tượng Set. Các đối tượng được so sánh theo tham chiếu, không phải theo giá trị.

Giá trị trả về

Đối tượng Set.

Ví dụ

Sử dụng add()

js
const mySet = new Set();

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

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

Đặc tả kỹ thuật

Specification
ECMAScript® 2027 Language Specification
# sec-set.prototype.add

Tương thích trình duyệt

Xem thêm