vendredi 16 juin 2017

Unusual if/else behaviour [duplicate]

This question already has an answer here:

I have an array of two nested arrays.

var array = [ [a,b,c], [a,b,c] ]

Despite the array elements being identical, the following code returns true:

if (array[0] !== array[1]) {
  console.log(array[0])
  console.log(array[1])
}

// [a,b,c]
// [a,b,c]

And the following code returns false:

if (array[0] === array[1]) {
  console.log(array[0])
  console.log(array[1])
}

It seems to be comparing the indices instead of the elements.

What is going on here?

Aucun commentaire:

Enregistrer un commentaire