Featurecore
Key and value equality of Map and Set
Also called SameValueZero, Map key equality, Set value equality, NaN as a Map key, -0 as a Map key, object keys by reference.
Map and Set compare keys with SameValueZero: like === but NaN equals NaN, and -0 is stored as +0. Objects match only themselves, so a key with the same contents is a different key.
- Language
- JavaScript
Synopsis
const m = new Map([[NaN, "nan"], [0, "zero"], ["1", "string one"]]);
console.log(m.get(NaN), m.get(-0), m.get(1), m.get("1"));
const s = new Set(["a", "a", 1, 1n, { k: 1 }, { k: 1 }]);
console.log(s.size);
const keys = [...new Map([[-0, "x"]]).keys()];
console.log(keys, Object.is(keys[0], -0));
nan zero undefined string one
5
[ 0 ] false
| Pair | === |
Object.is (SameValue) |
Map and Set (SameValueZero) |
|---|---|---|---|
NaN, NaN |
false | true | same key |
0, -0 |
true | false | same key |
1, '1' |
false | false | different keys |
1, 1n |
false | false | different keys |
| two objects with the same contents | false | false | different keys |
Semantics
r1. SameValueZero "determines whether or not the two arguments are the same value (ignoring the difference between +0๐ฝ and -0๐ฝ)."1 Its first step is "If SameType(x, y) is false, return false."1 MDN: "Same-value-zero only differs from strict equality by treating NaN as equivalent, and only differs from same-value equality by treating -0 as equivalent to 0 ."2
r2. Map and Set use it. For Map, "Distinct key values are discriminated using the semantics of the SameValueZero comparison algorithm", and for Set, "Distinct values are discriminated using the semantics of the SameValueZero comparison algorithm."3 The Guide sums it up: "-0 and +0 are considered equal. NaN is considered equal to itself (contrary to === )."4 Example: rules.
r3. A key of -0 is stored as +0. set, get, has, delete and add first call CanonicalizeKeyedCollectionKey, whose step is "If key is -0๐ฝ, return +0๐ฝ."3 So keys() gives back 0, not -0, as the rules example shows with Object.is.
r4. Objects are compared by identity. None of the equality operations "compares whether the parameters are conceptually similar in structure. For any non-primitive objects x and y which have the same structure but are distinct objects themselves, all of the above forms will evaluate to false ."2 Primitives compare by value, so two strings with the same characters are one key. Example: identity.
r5. The same comparison is used elsewhere: "It's used by Array.prototype.includes() , TypedArray.prototype.includes() , as well as Map and Set methods for comparing key equality."2 Strict equality "is also used by array index-finding methods including Array.prototype.indexOf()", so "you cannot use indexOf(NaN) to find the index of a NaN value in an array".2 Example: includes.
r6. To use a value with structure as a key, a program keeps the object itself, or derives a primitive key from it. MDN names the cost of the second way: "you must maintain the mapping between the original object and its representation as the object changes."5 Example: compound.
Examples
One object is one key, wherever it is used (rule r4).
const point = { x: 1, y: 2 };
const labels = new Map([[point, "A"]]);
point.x = 10;
console.log(labels.get(point), labels.get({ x: 10, y: 2 }));
const first = "ab";
const second = "a" + "b";
console.log(new Set([first, second]).size);
A undefined
1
Changing point does not move its entry: the map holds the object, not its contents.
includes and indexOf on the same array (rule r5).
const values = [1, NaN, -0];
console.log(values.includes(NaN), values.indexOf(NaN));
console.log(new Set(values).has(NaN), values.includes(0));
true -1
true true
A string key for a pair of numbers, and a map of maps (rule r6).
const grid = new Map();
grid.set(`${3},${4}`, "tree");
console.log(grid.get(`${3},${4}`), grid.get([3, 4]));
const nested = new Map();
const row = nested.get(3) ?? new Map();
nested.set(3, row.set(4, "tree"));
console.log(nested.get(3)?.get(4), nested.get(5)?.get(4));
tree undefined
tree undefined
Pitfalls
An array as a key
An array literal is a new object each time, so it never finds an entry made with another array (rule r4).
This code runs but gives a wrong answer on purpose.
const seen = new Set();
seen.add([1, 2]);
console.log(seen.has([1, 2]), seen.size);
seen.add([1, 2]);
console.log(seen.size);
false 1
2
Use a string key such as '1,2', or keep the array in a variable and pass the same array each time.
Numbers and numeric strings as keys
An object turns the key 1 into '1', and a Map does not (rule r1). Code that moves from objects to maps can miss entries.
This code runs but gives a wrong answer on purpose.
const obj = {};
obj[1] = "one";
console.log(obj["1"]);
const map = new Map();
map.set(1, "one");
console.log(map.get("1"), map.get(Number("1")));
one
undefined one
Comparison
Python compares dict keys by value, through hashing: "Values that compare equal (such as 1 , 1.0 , and True ) can be used interchangeably to index the same dictionary entry."6 In JavaScript, 1 and '1' or 1 and 1n are different keys (rule r1). Java maps use equals, so a class decides what an equal key is: containsKey is specified as "returns true if and only if this map contains a mapping for a key k such that (key==null ? k==null : key.equals(k)) ."7 Go requires that "The comparison operators == and != must be fully defined for operands of the key type".8 A JavaScript Map has no hook like equals: SameValueZero is fixed (rule r2). See Use a custom key in a map.
References
- ECMAScript 2026 Language Specification (ECMA-262, 17th edition), clause 7, 7.2.9 SameValue; 7.2.10 SameValueZero
- MDN Web Docs, JavaScript Guide, Equality comparisons and sameness, the introduction; Same-value-zero equality
- ECMAScript 2026 Language Specification (ECMA-262, 17th edition), clause 24, 24.1 Map Objects; 24.2 Set Objects; 24.5.1 CanonicalizeKeyedCollectionKey
- MDN Web Docs, JavaScript Guide, Keyed collections, Key and value equality of Map and Set
- MDN Web Docs, JavaScript Reference, Map.groupBy(), Description
- The Python Standard Library, Python 3.14, Built-in Types, Mapping Types, dict
- Java SE 25 API, Interface Map, the interface description
- The Go Programming Language Specification, Map types
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag 47M4KN Edit this page History Report an error Markdown