JavaScript: How Map Determines Uniqueness of Keys

By Xah Lee. Date: . Last updated: .

The equality test used for determining whether 2 keys in a Map Object is the Triple Equal Operator except treatment of NaN. NaN === NaN return false, but for map object, NaN is considered same as any NaN.

// NaN === NaN is false
console.log((NaN === NaN) === false);

// for map object, NaN key are considered the same
const xx = new Map();
xx.set(NaN, "y1");
xx.set(NaN, "y2");

console.log(xx.size === 1);
console.log(xx.get(NaN) === "y2");

JavaScript Map Object

BUY ΣJS JavaScript in Depth