Arr.includes(item) - Problem with Multidimensional Arrays

I’m trying to use the arr.includes(item). The function should return True if the item is an element of the array. But it doesn’t seem to be able to do so with a multidimensional array. Take a look at this screenshot (running node in the console):

Is it because it’s an EC6 function, and not yet completely functional?

No, it’s because you need to pass the exact value (string or number) or a reference to the exact same object/array which you want the include function to test against. In this case, you are passing a completely new array which holds the same values as an array in your multidimensional array.

These are not the same thing.

To demonstrate, try:

console.log( [0,1] ===[0,1]);

That will output false, because that’s a strict equal comparison and the two arrays, while holding the same values, are not the same data structure in memory.

Try this instead:

array_1 = [0,1];
array_2 = [1,2];
const multi_arrays = [array_1,array_2];
multi_arrays.includes(array_1);

That will output true because you are passing in a reference to an array and the multi_arrays array contains a reference to that array as well.

Hope that helps!
~Micheal

2 Likes

Thank you very much.
To put it in other words, I’m passing a reference/address, and the addresses are not equal.

1 Like