What is the difference between “in operator” and “includes()” for JavaScript arrays

Array.includes() checks for the existence of a value in an array, while the in operator checks for the existence of a key in an object (or index in the case of arrays).

Example:

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

Leave a Comment