Your product could be here, in front of thousands of JavaScript developers. Sponsor this spot →
JavaScript Quiz

Why can a Map have both 1 and "1" as keys?

Do you know what will be the output of this code?

JavaScript
const map = new Map();
map.set(1, "number");
map.set("1", "string");
console.log(map.size);
Show answer and explanation

The answer is 2.

Map compares keys with SameValueZero, so the number 1 and the string "1" are different keys. A plain object would convert both to "1".

Take the full quiz Next question →

More Objects questions

  1. Does the spread operator make a deep copy?
  2. When do default parameters apply in JavaScript?
  3. What is the difference between ?? and || in JavaScript?
  4. How does optional chaining short-circuit in JavaScript?
  5. When do destructuring default values apply?

Related topics