본문 바로가기

JavaScript/methods

[JavaScript] Array.prototype.includes()



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