1. includes()
- includes() 메서드는 특정 문자열이 어떤 문자열에 포함되어 있는지 판별
- 결과를 true 또는 false로 반환
구문
str.includes(searchString)
str.includes(searchString, position)
매개변수
- searchString
- 찾으려는 문자열
- position (선택사항)
- 문자열에서 찾기 시작하는 위치를 나타내는 index 값
- 기본값은 0이며, 문자열 전체에서 찾게 된다.
반환값
- searchString이 있으면 true, 없으면 false이다.
let ice = ['예은', '지수', '희수', '서아', '수진', '예지'];
let friend = ['수민', '인정', '민송'];
ice.includes('예은') // true
friend.includes('예은') // false
2. indexOf()
- includes() 메서드는 특정 문자열이 어떤 문자열에 포함되어 있는지 판별한다.
- 결과를 true 또는 false로 반환한다.
구문
str.includes(searchString)
str.includes(searchString, position)
매개변수
- searchString
- 찾으려는 문자열
- position (선택사항)
- 문자열에서 찾기 시작하는 위치를 나타내는 index 값
- 기본값은 0이며, 문자열 전체에서 찾게 된다.
반환값
- searchString이 있으면 true, 없으면 false이다.
let ice = ['예은', '지수', '희수', '서아', '수진', '예지'];
let friend = ['수민', '인정', '민송'];
ice.includes('희수') // 2
friend.includes('희수') // -1
indexOf()와 includes()의 공통점
- 어떤 문자열에 특정 문자열이 존재하는지 확인할 때 사용할 수 있다.
indexOf()와 includes()의 차이점
- indexOf()는 특정 문자열이 존재하는 첫 번째 index도 알 수 있지만, includes()는 알 수 없다.
- includes()는 비교적 최신에 나온 문법이어서 Internet Explorer에서 작동하지 않는다.
3. some
- some 메서드는 해당하는 요소를 발견하는 즉시 true를 반환
- 그렇지 않고 모든 요소가 거짓을 반환하면 false를 반환
const array = [1, 2, 3, 4, 5];
// callback(element, index, array)
array.some(v => v === 3); // true
array.some(v => v > 5); // false
- some 메서드는 object가 담겨있는 배열에서 includes와 indexOf와 다르게 더욱 유용하게 사용 가능
4. every
const array = [1, 2, 3, 4, 5];
// callback(element, index, array)
array.every(v => v > 0); // true
array.every(v => v === 3); // false
array.every(v => v > 3); // false
- every 메서드는 모든 요소가 판별 조건에 일치하면 true를 반환
- 그렇지 않으면 false를 반환
const array = [{name: 'red'}, {name: 'green'}, {name: 'yellow'}];
array.every(v => v.name.length > 2); // true
array.every(v => v.name === 'green'); // false
- every 메서드는 object가 담겨있는 배열에서 includes와 indexOf와 다르게 더욱 유용하게 사용 가능
'JS' 카테고리의 다른 글
[JS]BigInt (0) | 2023.01.31 |
---|---|
[JS] == , === 차이점 (0) | 2023.01.18 |
[JS] 배열에서 특정 값의 개수 구하기 (0) | 2023.01.17 |
[JS] Templete Literal : 백틱(``), 달러(${}) (0) | 2023.01.16 |
[JS] Javascript 이론 (1) (0) | 2023.01.14 |