0

I'm working with JavaScript and I had this bug, the cause was that "0" and !"0" entered in the "then" sentence. When I tried in a console I saw:

!"0" == "0" -> true

Why did this happen?

Frederic
  • 2,015
  • 4
  • 20
  • 37
Federico Sawady
  • 900
  • 5
  • 12
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence – Teemu Jan 22 '18 at 17:49
  • Because `(!("0" == "0")) != ((!"0") == "0")`. – jonrsharpe Jan 22 '18 at 17:50
  • 1
    Possible duplicate of [Which equals operator (== vs ===) should be used in JavaScript comparisons?](https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons) – Omar Himada Jan 22 '18 at 17:51
  • 3
    Possible duplicate of [In JavaScript, why is "0" equal to false, but when tested by 'if' it is not false by itself?](https://stackoverflow.com/questions/7615214/in-javascript-why-is-0-equal-to-false-but-when-tested-by-if-it-is-not-fals) – jonrsharpe Jan 22 '18 at 17:52
  • Why does this matter? Is this an actual problem in code? Did you see this somewhere else? If so, what was the context? Didn't they give an explanation for this seemingly useless bit of code? –  Jan 22 '18 at 18:05

4 Answers4

3

!"0" converts the string "0" into a boolean, and takes its negation. So since "0" is truthy (the only falsy string is the empty string), its negation is the boolean value false. So to be clear:

!"0" is converted to false.

Since we are comparing the boolean false with the string "0", javascript will convert both values to numbers. See the table in this documentation to see which types, when compared, are converted to what. You'll note that for a boolean and string, both are changed to numbers and then compared.

So wrapping up, we have !"0", which evaluates to false. Then we compare that to the other string: "0". Because one is a boolean, and the other a string, they are converted to numbers. The number conversion for false is 0, and the number conversion for "0" is 0, which are equal. Thus we get the result you see.

CRice
  • 29,968
  • 4
  • 57
  • 70
  • "0" is not truthy, "0" == false -> true, but your answer is correct – Federico Sawady Jan 22 '18 at 18:10
  • But why !"0" evaluates to false? – Federico Sawady Jan 22 '18 at 18:12
  • 2
    `"0"` is truthy, but being truthy *is not the same as being `==` to `true`*. When you compare it directly to a boolean, as before, they are converted to numbers. So `"0" -> 0` and `false -> 0` and `0 === 0`, so you get true. – CRice Jan 22 '18 at 18:12
  • Mind blown, I always think that truthy is exactly being equal to true. – Federico Sawady Jan 22 '18 at 18:23
  • 1
    I guess you could say `x` is truthy *if and only if* `Boolean(x) === true`, where `Boolean` is the built-in javascript function. So then you could note that `Boolean("0")` does return `true`. Pretty neat. – CRice Jan 22 '18 at 18:27
0

One would say, its javascript. There is a logical explanation:

  • !"0" evaluates to false
  • "0" will be casted to 0 which is equal to false

Since you are now comparing false and false, they are both the same. Your comparison has to be true.

rollstuhlfahrer
  • 3,988
  • 9
  • 25
  • 38
0

This is why JavaScript coercion is tricky and plays very handy part specifically in case of 0 number which is 99% the bug cause.

  • When Equality (==) comparison is applied on two values e.g x == y by default comparison value is checked from L->R (left to rigth).

  • So In your case before Loose (==) equality is checked the ! (negation is applied) which results !"0" -> false.

  • Now comes the coercion part: According to ECMAScript ecma-262 specs it states

    • If Type(x) is Boolean, return the result of the comparison ! ToNumber(x) == y.

      OR

    • If Type(y) is Boolean, return the result of the comparison x == ! ToNumber(y).

Which means if either of the value is in Boolean like in our case

-> false == "0"

-> Number(false) == "0"

-> Number(false) == Number("0")

-> 0 == 0 // Results into truthy values;

Therefore:
!"0" == "0" -> true
khizer
  • 1,242
  • 15
  • 13
-1

!"0" will convert "0" to false and then false is == to "0" because they will both be converted to the integer 0. https://stackoverflow.com/a/7615326/4949918

Axnyff
  • 9,213
  • 4
  • 33
  • 37