I came across this quirk while trying to optimise string pluralisation in a game of code golf. I had the idea to write strings as plurals and then use substr
to cut the last character off, conditionally:
var counter = 1;
var myText = counter + " units".substr(0, 6-(counter===1));
It's fine - it does what I wanted. But looking at the MDN docs for String.prototype.slice(), I thought I had found a way to make it even shorter, by using passing negative zero as the second argument to the function. From the docs:
endSlice
Optional. The zero-based index at which to end extraction. If omitted, slice() extracts to the end of the string. If negative, it is treated as sourceLength + endSlice where sourceLength is the length of the string (for example, if endSlice is -3 it is treated as sourceLength - 3).
var myText = counter + " units".slice(0,-(counter===1));
This evaluates to .slice(0,-1)
when counter
is equal to 1, which would chop the last letter from the string, or it would otherwise evaluate to .slice(0,-0)
, which according to the docs should mean 0 characters were subtracted from the length of the string being operated upon.
As it happens, -0 is treated the same as +0 by String.prototype.slice
. I wondered if this was a convention, to treat -0 as being the same as +0 (I know, for instance, -0 === +0
evaluates to true
). I thought to look at String.prototype.substr
, but +0 and -0 are supposed to be handled the same way in that function.
Does anyone have any greater insight into this? Is there some basic convention in the language design that states that, while signed zero is a language feature, it should be ignored, except in certain scenarios (like 1/-0
)?
tl;dr I'm salty that I can't make jokes about winning code golf by slicing.
if (val < 0)
, and given that-0 < 0
returnsfalse
...as it should: imagine if we had to write all of our code to distinguish between-0
and+0
... – Bridget-0
is just a floating point representation of real world zero, just like+0
. – Bork1/-0 === -Infinity
whereas1/0 === Infinity
– Astoniedval < 0
did not work for "normal" cases that would lead to all sorts of bugs. – Bridget+0
and-0
all the time would be headache-inducing. – Astonied