1

It probably was asked before but I could't find it. I read the specs too, didn't see anything bizzare that would explain why "0" evaluates to true.

So why would "0" ? "yes" : "no" return yes?

("0"==true) ? "yes" : "no" works as expected.

serg
  • 109,619
  • 77
  • 317
  • 330
  • So you didn't see this post? http://stackoverflow.com/q/359494/1026459 – Travis J Jan 05 '15 at 23:56
  • @TravisJ No, but now that I've seen it, it still doesn't explain it to me. Under what circumstances would `"0"` evaluate to `true`? – serg Jan 05 '15 at 23:59
  • @serg I listed the link in my answer, but this has to do with something called type coersion. You can read more about it here: http://webreflection.blogspot.com/2010/10/javascript-coercion-demystified.html – ravendano Jan 06 '15 at 00:07
  • See also my answer here: http://stackoverflow.com/q/7496727/218196 – Felix Kling Jan 06 '15 at 00:38
  • It's a good question because (x) and (x==true) ought to be the same thing, but this is a case where they are not. – Octopus Jun 26 '18 at 21:18

2 Answers2

4

Non-empty strings are truthy. "0" is not 0.

However, comparison will coerce 0 to a number.

Note, the only string which can be coerced to true during comparison is "1". (Please let me know if there are edge cases I'm missing!)

"true" == true // false
"foo"  == true // false
"0"    == true // false
"1"    == true // true
Evan Davis
  • 35,493
  • 6
  • 50
  • 57
  • Strings are truthy? ""? ;-) – alexander farkas Jan 05 '15 at 23:56
  • `"0"==true` is `false` – serg Jan 05 '15 at 23:56
  • 2
    But `!!"0"` is `true`. – Cymen Jan 05 '15 at 23:58
  • 2
    That's because `Boolean("0") === true` but `"0" !== String(true)`. – Peter Olson Jan 06 '15 at 00:00
  • Yes that because !!"0" performs a type coercion to force the statement into a true statement. This does not occur for "0" == true – ravendano Jan 06 '15 at 00:00
  • Ok, the answer I was looking for is that `Boolean("0") === true`, that explains it. – serg Jan 06 '15 at 00:11
  • 1
    @PeterOlson: `"0" !== String(true)` is not the comparison that is performed. It's `Number("0") == Number(true)`. – Felix Kling Jan 06 '15 at 00:37
  • 1
    *"Note, the only string which can be coerced to `true` [...]"* That's not very precise. Coerced how? `Boolean("foo")` is `true` as well. If you mean "the only string (loosely) equal to the value `true`", then you are right. However, that's because both values are actually converted to numbers. The string is never converted to a boolean in that process. See my previous comment. – Felix Kling Jan 06 '15 at 00:41
  • @FelixKling I added the qualifier "_during comparison_", but your explanation perfectly covers what I was trying to say. Thanks! – Evan Davis Jan 06 '15 at 01:02
1

If a string has atleast one character then the string will evaluate to truthy. As a result your first example will return "yes".

If you had used something like the following however it would have returned no:

"" ? "yes" : "no" // this evaluates to "no" since an empty string is considered falsey

This is a direct result of how type coercion occurs in javascript. I would encourage you to checkout the following link for more information on what type coercion is and how it works: Type Coercion

ravendano
  • 346
  • 1
  • 3
  • *"Your second example does not return true because type coercion does not occur."* Actually it does. In `"0" == true`, both values are coerced to numbers. – Felix Kling Jan 06 '15 at 00:39