Set() 생성자
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월.
Set()
생성자는 Set
객체를 생성합니다.
시도해 보기
const set1 = new Set([1, 2, 3, 4, 5]);
console.log(set1.has(1));
// Expected output: true
console.log(set1.has(5));
// Expected output: true
console.log(set1.has(6));
// Expected output: false
구문
js
new Set()
new Set(iterable)
참고:
Set()
은 오직new
와 함께 생성할 수 있습니다.new
없이 생성을 시도하면TypeError
가 발생합니다.
매개변수
iterable
Optional-
순회 가능한 객체가 전달되면 모든 요소가 새로운
Set
에 추가됩니다.이 매개변수를 지정하지 않거나 값이
null
일 경우 새로운Set
은 비어있게 됩니다.
반환 값
새로운 Set
객체.
예제
>Set
객체 사용하기
js
const mySet = new Set();
mySet.add(1); // Set [ 1 ]
mySet.add(5); // Set [ 1, 5 ]
mySet.add(5); // Set [ 1, 5 ]
mySet.add("some text"); // Set [ 1, 5, 'some text' ]
const o = { a: 1, b: 2 };
mySet.add(o);
명세서
Specification |
---|
ECMAScript® 2026 Language Specification> # sec-set-constructor> |
브라우저 호환성
Loading…