includes() 메서드는 배열이 특정 요소를 포함하고 있는지 판별합니다.
리턴 값은 Boolean 입니다.
const array1 = [1, 2, 3];
console.log(array1.includes(2));
// Expected output: true
const pets = ['cat', 'dog', 'bat'];
console.log(pets.includes('cat'));
// Expected output: true
console.log(pets.includes('at'));
// Expected output: false
참고로 string 타입에서도 쓸 수 있는 메서드 입니다.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes
String.prototype.includes() - JavaScript | MDN
The includes() method of String values performs a case-sensitive search to determine whether a given string may be found within this string, returning true or false as appropriate.
developer.mozilla.org
'JavaScript > methods' 카테고리의 다른 글
[JavaScript] for of 반복문에서 배열의 idx 값 가져오기 entries() (0) | 2024.08.29 |
---|---|
[JavaScript] Array.from() (0) | 2024.08.26 |
[JavaScript] Object.keys, Object.values, Object. entries, Object.fromEntries (0) | 2024.08.25 |
[JavaScript] with 사용법 Array.prototype.with() (0) | 2024.08.25 |
[JavaScript] 배열 정렬 Array.prototype.sort() (0) | 2024.08.25 |