JS: How Map Determines Uniqueness of Keys

By Xah Lee. Date: . Last updated: .

How Map Determines Uniqueness of Keys

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");

How Set Determines Uniqueness

The equality test used for determining whether 2 values in a set is the same as ===, except treatment of NaN (not a number).

NaN === NaN return false, but for set object, NaN is considered same as any NaN.

// equality test used for determining whether 2 values in the set is the same as ===, except treatment of NaN

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

// but for set object, NaN is same as any NaN
const ss = new Set([NaN, NaN]);
console.log(ss.size === 1);

JavaScript, Map Object