The javascript includes
function can be used to find if an element is present in an array. Take the following example:
var arr = ['hello', 2, 4, [1, 2]];
console.log( arr.includes('hello') );
console.log( arr.includes(2) );
console.log( arr.includes(3) );
console.log( arr.includes([1, 2]) );
Passing 'hello'
or 2
to the function returns true
, as both are present in the array arr
.
Passing 3
to the function returns false
because it is not present in the array.
However, why does arr.includes([1, 2])
return false
as well, even though this is equal to the last element in the array? And if this method does not work, how else can I find whether my array includes the item [1, 2]
?