-1

the string of false "false" is not true or false:

"false" == true // false

"false" == false // false

how is this possible? this does not apply to "true". The above results were executed in the chrome console panel.

EDIT

If the above does not evaluate to true then why does the bellow statement print hello:

if("false") console.log("hello")

EDIT2

In the linked duplicate it says "false" is converted to NaN. Let's test this in chrome shell:

if(NaN) console.log("hello")

if("false") console.log("hello")
// prints hello

But weirdly enough isNaN("false") returns true.

Arijoon
  • 2,184
  • 3
  • 24
  • 32
  • To get the string of `false`, you should use `String(false)`. That'll get you the expected result. – Bergi Jun 10 '18 at 15:29
  • https://stackoverflow.com/questions/3976714/converting-string-true-false-to-boolean-value, https://stackoverflow.com/questions/23404028/what-are-the-pros-and-cons-of-using-boolean-type-versus-string-type-true-false – Bergi Jun 10 '18 at 15:33
  • A string is a string. A Boolean is a Boolean. see [How can I convert a string to boolean in JavaScript?](https://stackoverflow.com/questions/263965/how-can-i-convert-a-string-to-boolean-in-javascript) – Ronnie Royston Jun 10 '18 at 15:40
  • @RonRoyston Sure, but "8" == 8 is true – FINDarkside Jun 10 '18 at 15:43
  • Right, but "8" === 8 is False. – Ronnie Royston Jun 10 '18 at 15:45
  • @RonRoyston Completely irrelevant since the question is not about the strict equality operator. – FINDarkside Jun 10 '18 at 15:47
  • Regarding your edits, using a value in an `if` condition is a very different thing than comparing it to a boolean with the not typesafe equality operator. – Bergi Jun 10 '18 at 16:09
  • doesn't an if statement check if that value evaluates to `true|false`? – Arijoon Jun 10 '18 at 23:57

2 Answers2

1

The empty string is falsey and every non-empty string is truthy.

"true" is not getting coerced to true, it's just an example non-empty string.

"0" is truthy (if ("0") { true } == true). But also "0" == false. That's because == coerces arguments to numbers and "0" becomes 0 and false becomes 0.

In general, never use the == operator in JS. Use === instead. If you want to use truthiness/falsiness of other values be careful :)

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
cmc
  • 973
  • 8
  • 17
0

This does apply to "true" as well. Here "false" and "true" are same as any other string value in javascript, it does not have anything with boolean true or false value. Hence,

"true" == true is false
"true" == false is false
"false" == true is false
"false" == false is false
Hriday Modi
  • 2,001
  • 15
  • 22
  • Some string values like `"1"` and `"0"` do have something to do with boolean values though. – Bergi Jun 10 '18 at 15:37
  • @Bergi Agreed "1" and "0" have something with boolean values and it is very well expained here: https://stackoverflow.com/questions/11363659/why-does-true-true-show-false-in-javascript – Hriday Modi Jun 10 '18 at 15:51