1

Must say before all that my basic knowledge comes from java and not js. I have the following map:

const myMap = new Map();
myMap.set([1,2], 'Test');
myMap.get([1,2]// return undefined

can't understand why the last line of code doesn't return 'Test'. in java the key is being checked using equals method, how does js checks for equality? I'm not looking for the solution that will return 'Test', only the explantion why it is not return 'Test' for this specific code?

Eitanos30
  • 1,331
  • 11
  • 19
  • 2
    `[1,2] !== [1,2]`. – ASDFGerte Dec 26 '20 at 16:08
  • why? they are both the same type and the same values!!! – Eitanos30 Dec 26 '20 at 16:10
  • How precisely a map does equality on keys can be read on the [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map#Key_equality) or the spec. For why the above holds, because with reference equality, they are two separate instances of an array - the same values inside, but still two. – ASDFGerte Dec 26 '20 at 16:12
  • i have checked and [1,2] == [1,2] also return false. It is super not understandable – Eitanos30 Dec 26 '20 at 16:14
  • There are hundreds of articles that explain this in detail and with many examples, better than i can and/or would be willing to repeat in a comment - maybe someone has a link to a good dupe target, or a tutorial. – ASDFGerte Dec 26 '20 at 16:16

0 Answers0