2

I have not found yet any discussion or article about falsy values in Javascript where minus zero is mentioned while I see !!-0 yielding false. Why is that?

Update: I now think most authors summing up falsy values either do not know about -0 being a value or mean by 0 both +0 and -0.

Marco de Wit
  • 2,686
  • 18
  • 22
  • 4
    @AnkitaKuchhadiya `-0` and `+0` are two distinct values in JS – VLAZ Dec 26 '19 at 08:59
  • 2
    `-0` is `false`. There's little to distinguish it from `+0` other than checking which `Infinity` results when using it as a denominator in division. – Patrick Roberts Dec 26 '19 at 09:00
  • note that +0===-0 – Dmitry Krivolap Dec 26 '19 at 09:01
  • 2
    @Marco yeah `0` typically encompasses both `-0` and `+0` because the behaviour in almost all cases is the same. The negative zero value comes into play so rarely that specifically mentioning it in order to say "it works the same as positive zero" is a waste of everyone's time - the reader and the writer of something. Hence why `0` is an acceptable shortcut that means "either zero" – VLAZ Dec 26 '19 at 09:01
  • Does this answer your question? [How to check the value given is a positive or negative integer?](https://stackoverflow.com/questions/7037669/how-to-check-the-value-given-is-a-positive-or-negative-integer) – Ramesh Rajendran Dec 26 '19 at 09:03
  • 2
    @DzmitryKrivolap but `Object.is(+0, -0)` returns false – adiga Dec 26 '19 at 09:03
  • 1
    It also may be interesting. https://medium.com/javascript-in-plain-english/why-does-javascript-have-0-9b6e1965a075 – Dmitry Krivolap Dec 26 '19 at 09:04
  • Kindly look : https://hackernoon.com/negative-zero-bbd5fd790af3 – Ramesh Rajendran Dec 26 '19 at 09:07
  • 1
    IIRC the spec actually mentions that (briefly) *if the value is +0 or -0, return false* in the abatract `ToBoolean` operation. – Jonas Wilms Dec 26 '19 at 09:12

3 Answers3

6

Simply put, the ECMAScript specification states that the boolean conversion of -0 results in false. This is stated in the table of conversions for the abstract operation ToBoolean, section 7.1.2.

If argument is +0, -0, or NaN, return false; otherwise return true.

Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
0

Here is why: the -0 here is first calculated. It won't calculate just the negative sign itself, thus it includes the digit. It then outputs 0 Then it is run through the ! operator two times. The first ! operator converts or parses 0 into a boolean, and then inverts or falsifies the boolean. !0 returns true The second inverts it again, returning false

Kino Bacaltos
  • 373
  • 1
  • 2
  • 16
  • "*It then outputs `0`", incorrect: `-0` results in a *negative zero*, it'd [a different value than `0`](https://stackoverflow.com/questions/7223359/are-0-and-0-the-same) – VLAZ Dec 26 '19 at 09:17
0

This is a very good question. Javascript does distinguish -0.0 from 0.0. (And IEEE754 does too.) We can test this with:

var X = -0.0; X
-> "-0"

So it is a value distinct from 0.0.

`!-0.0` -> true

So you are right, lists of falsy values should explicitly list -0.0 too.

Probably they dont list it because:

0.0 === -0.0   -> true

However that is not a good reason if we consider that javascript is inconsistent in some relations (e.g. non-transitive).

Bernd Elkemann
  • 23,242
  • 4
  • 37
  • 66