The JavaScript includes
function tells you whether 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
, since each of these is present in the array arr
.
Passing 3
to the function returns false
because this is not present in the array.
However, why does arr.includes([1, 2])
also return false
, 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]
?