Set.prototype.has()

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.

has() 메서드는 Set 객체에 주어진 요소가 존재하는지 여부를 판별해 반환합니다.

시도해보기

구문

js
mySet.has(value);

매개변수

value

Set 객체에서 존재 여부를 판별할 값.

반환 값

Set 객체에 값이 존재하면 true, 아니면 false.

참고: 기술적으로, has()sameValueZero 알고리즘을 사용해 요소의 존재 여부를 판별합니다.

예제

has() 사용하기

js
var mySet = new Set();
mySet.add("foo");

mySet.has("foo"); // true
mySet.has("bar"); // false

var set1 = new Set();
var obj1 = { key1: 1 };
set1.add(obj1);

set1.has(obj1); // true
set1.has({ key1: 1 }); // false, 형태만 같은 서로 다른 객체의 참조이기 때문
set1.add({ key1: 1 }); // set1의 요소가 2개로 늘어남

명세

Specification
ECMAScript Language Specification
# sec-set.prototype.has

브라우저 호환성

BCD tables only load in the browser

같이 보기