includes

Tags
테스트
bool
요약
배열이 특정 요소를 포함하고 있는지 판별 ⇒ T/F
arr.includes(valueToFind[, fromIndex])
  • 대소문자 구분함
  • 일부내용 검색은 안됨

인자

valueToFind
: 탐색할 요소.
fromIndex Optional
: 검색 시작 위치
: 음의 값은 array.length + fromIndex의 인덱스를 asc로 검색합니다. 기본값은 0입니다.
const array1 = [1, 2, 3]; array1.includes(2); // true array1.includes(4); // false array1.includes(3, 3); // false --- 인덱스 3에는 아무것도 없으므로 false [1, 2, 3].includes(3, -1); // true [1, 2, NaN].includes(NaN); // true
const pets = ['cat', 'dog', 'bat']; console.log(pets.includes('cat')); // expected output: true console.log(pets.includes('at')); // 일부검색은 안됨 // expected output: false

검색 위치로 음수 전달 시

// array length is 3 // fromIndex is -100 // computed index is 3 + (-100) = -97 var arr = ['a', 'b', 'c']; arr.includes('a', -100); // true // 음수가 배열 길이를 넘으면 배열 처음부터 찾는 거랑 같음. arr.includes('a', -2); // false // 끝에서 2번째 요소부터만 검색함.