본문 바로가기

Front-End: Web/JavaScript

[js] includes vs in

반응형

Array.includes()

array 내 값이 존재하는지 확인

in operator

객체 내 key가 존재하는지 확인

(array의 경우, 해당 index가 존재하는지 확인)

 

예제

var arr = [5];

console.log('Is 5 is an item in the array: ', arr.includes(5)); // true
console.log('do we have the key 5: ', 5 in arr); // false
console.log('we have the key 0: ', 0 in arr); // true

 


참고 자료

https://stackoverflow.com/questions/48499181/what-is-the-difference-between-in-operator-and-includes-for-javascript-arr

반응형