1

I have the following JavaScript/jQuery code:

if (isVariance && value)
    tableCell.addClass('NonZeroVariance');

Where:

isVariance == true and value == "0.00".

(isVariance && value) == "0.00".

(isVariance && !!value) == true.

The if condition evaluates to true, and the class is added to tableCell.

So, my expectation was that zero would be interpreted as false, and that "0.00" would be evaluated as false. But that's not what happens here. Can someone tell me why?

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • I do know that `==` will check against all types, where `===` is true validator for equality in Javascript. – Greg Oct 24 '14 at 18:55
  • The string `"0.00"` is truthy. The *number* `0.00` is not. So `if (true && "0.00") { console.log("true"); }` logs true while `if (true && 0.00) { console.log("true"); }` does not. – Matt Burland Oct 24 '14 at 18:57
  • @MattBurland `false == "0.00"` in the console outputs `true` – andrew Oct 24 '14 at 18:58
  • @andrew: Yes, but that's not the same thing. For example, if you do `if (true && "0.00" == true) { console.log("true"); }` then it will not log true. See: https://developer.mozilla.org/en-US/docs/Glossary/Truthy – Matt Burland Oct 24 '14 at 19:00
  • **NOTE:** The question quoted as already answering this one does, in fact, answer this question. But the question itself is very different and less likely to be found by someone with my question. I think this question should be reopened. – Jonathan Wood Oct 24 '14 at 19:39
  • No, it shouldn't be reopened. This sort of question has been handled many times over. You've simply obfuscated it with an irrelevant `isVariance && ` operation. The question is about `if (value) {` where `value` is `"0.00"`. But [here's another duplicate](http://stackoverflow.com/questions/7615214/in-javascript-why-is-0-equal-to-false-but-not-false-by-itself) that uses an `if` statement if you really think it matters. –  Oct 24 '14 at 20:43
  • *"It's got nothing to do with `isVariance &&`"* Exactly. It has entirely to do with the "truthy" evaluation of `value`, which is `"0.00"`, and that's precisely what both other questions are dealing with. Neither this `('0' ? 'a' : 'b')` nor this `if ("0")` are dealing with equality. Yes, I downvoted this question because these issues are very basic and have been covered many, many times before. –  Oct 25 '14 at 01:43
  • @squint: I disagree. And if you don't like my question, then stop posting on it and go get bent. – Jonathan Wood Oct 25 '14 at 01:52
  • @JonathanWood: What a fragile little person you seem to be. If you don't like my comments, stop sending me notifications. But I guess you just want me to tell me how great you are. Alright then. Wow, great question! You're really breaking new ground here! No one has ever wondered about boolean evaluations of values in a dynamically typed language before! Well done! –  Oct 25 '14 at 02:23

1 Answers1

2

Yes, it appears that value is a string. And while the number 0.00 will evaluate to false, the string "0.00" evaluates to true.

The solution is to first convert the string to a number, and then perform the same test.

value = Number(value);
if (isVariance && value)
    tableCell.addClass('NonZeroVariance');

EDIT:

The reason that the string "0.00" evaluates to true, is because: The result is false if the argument is the empty String (its length is zero); otherwise the result is true. - http://javascriptweblog.wordpress.com/2011/02/07/truth-equality-and-javascript/

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • You didn't answer your own question *"Can someone tell me why?"*. You just reiterated what you had already demonstrated in the question and then gave a solution, which may or may not work in some cases. –  Oct 24 '14 at 19:18
  • 1
    Right, and you already demonstrated that in the question. `(isVariance && value) == "0.00"` `(isVariance && !!value) == true` Either way, these questions of truthy/falsey evaluation of values have been asked many, many times before. –  Oct 24 '14 at 19:34
  • 1
    ...and keep in mind that since we know that `isVariance` is `true`, and since you're using `&&`, that means we know that the value of `value` is going to be the result of the `&&` operation no matter what *(whether truthy or falsey)*. This means that the question is no longer about simple truthy evaluation, but rather about the behavior of `==`, which isn't the same as a boolean evaluation. –  Oct 24 '14 at 19:38
  • ...oh, I forgot, you used a JavaScript operator in place of words for your single-line examples. That's not a good idea. It is about boolean evaluation, but the `isVariance &&` part is simply irrelevant. –  Oct 24 '14 at 19:42