Backstory/rationale for disallowing -0 in Map keys and Sets?

T.J. Crowder tj.crowder at
Wed Oct 10 10:42:49 UTC 2018

Negative zero (-0) is the only value you can't use as a Map key or a Set
value. Map's [`set`][1] and Set's [`add`][2] both explicitly replace it
with +0. MDN makes a point of saying that "early drafts" of the ES2015 spec
treated +0 and -0 as distinct, but that "changed" in ES2015. Looking at
some early drafts, at some stage Map and Set used SameValue (so you could
have both +0 and -0 as Map keys or in a Set), then went through a period
where you could specify a "comparator" for them (so a given instance would
use SameValue or SameValueZero). Then that was removed; the final spec uses
SameValueZero and has `set` and `add` replace -0 with +0.

Using SameValueZero and converting -0 to +0 when storing does have the
effect of ensuring consistency: You don't end up with two sets (for
instance) with different values (-0 and +0) that Set's comparison logic
(SameValueZero) considers the same. (But then, so would using SameValue and
converting from -0 to +0 earlier in the `set` and `add` algorithsm.) Using
SameValue and allowing -0 would also ensure consistency, but with the
ambiguity that -0 and +0 are often hard to distinguish (Number's `toString`
returns `"0"` for both, for instance; operations involving them largely
don't care about the pseudo-sign of 0). Is that the rationale? Basically,
avoid a footgun while ensuring consistency?


-- T.J. Crowder

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list