찾고자하는 특정요소가 배열내에 포함되는지 존재여부와 인덱스 정보를 알 수 있다. (모든 브라우져 호환 O)
// foods에 '치킨'이 있는지 확인하려면?
let foods = ['치킨', '피자', '보쌈'];
console.iog(foods.indexof('치킨')) // 0 // 해당 요소의 인덱스값 리턴
console.iog(foods.indexof('쌈장')) // -1 // 없는 요소는 -1 리턴
// '피자'가 있는지 없는지 확인하려면? 불리언값
console.iog(foods.indexof('피자') !== -1) // true
// foods.indexof('피자') 값이 -1이 아니라면? 배열에 있는 요소란 뜻이다.
console.iog(foods.indexof('똥') !== -1) // false
// 함수로 만든다면? function hasElement (배열, 찾으려는 엘리먼트(요소))
function hasElement (arr, element) {
let isPresent = arr.indexOf(element) !== -1 };
return hasElement(foods, '치킨')
// 바로 return arr.indexOf(element) !== -1 }; 해도 됨.
console.log(poods, '치킨') // true;
console.log(poods, '과자') // false;
특정 요소의 인덱스 정보는 알 수 없고, 배열에 포함되어 있는지 존재여부만 알 수 있다. (explorer 호환X)
//// foods에 '치킨'이 있는지 확인하려면?
let foods = ['치킨', '피자', '보쌈'];
console.iog(foods.includes('치킨')) // true;
console.iog(foods.includes('쌈장')) // fase;