Why is 'false' truthy in javascript?
Asked Answered
L

6

5

I understand that an empty string is falsy in javascript and a not-empty string is truthy in javascript.

However, why is 'false' truthy in javascript, is there anything explicit in the specification? Is it a performance issue or are there situations where you would want the string 'false' to represent true?

Leodora answered 22/9, 2015 at 15:9 Comment(3)
non empty string is truthy, whatever the chars it containsWouldst
@FrédéricHamidi can you add that as the answer, a lot of answers are the what, rather than a reason why.Leodora
Note that most languages that permit "truthy"/"falsey" values for strings do not try to actually parse the strings. Even in Bash, where false is a command that always returns a non-zero error code, the double-brackets if construct still evaluates the string false as "truthy": if [[ false ]]; then echo ha; fi. Only Perl, which attempts to treat strings as numbers whenever such a conversion might make sense, actually cares what the contents of a string are when determining its "truthiness", and even there "false" is not considered false ("0" and "undef" are, though).Ciracirca
A
8

Replying to the last part of your question:

Are there situations where you would want the string 'false' to represent true?

Let's consider I am testing for empty strings in my user input. To achieve that, I issue:

if (!theInput) {
    // Do something.
}

Now do I want that condition to be true if the user enters false in the text box? Of course, I don't.

Aleydis answered 22/9, 2015 at 15:14 Comment(0)
B
11

is there anything explicit in the specification?

Yes:

The result is false if the argument is the empty String (its length is zero); otherwise the result is true.

Broadtail answered 22/9, 2015 at 15:13 Comment(0)
A
8

Replying to the last part of your question:

Are there situations where you would want the string 'false' to represent true?

Let's consider I am testing for empty strings in my user input. To achieve that, I issue:

if (!theInput) {
    // Do something.
}

Now do I want that condition to be true if the user enters false in the text box? Of course, I don't.

Aleydis answered 22/9, 2015 at 15:14 Comment(0)
P
5

In Javascript any string that is not empty is truthy.

So, when evaluating any non-empty string results in true even when the string itself is 'false'.

You can read more about truthy and falsy values.

If you want to check a string for truthness, you can check it's length.

var val = str.length > 0;
Pulpy answered 22/9, 2015 at 15:10 Comment(0)
S
2

The value of a non-empty string is always true.

Boolean(false) returns false

Boolean('false') returns true

Sik answered 22/9, 2015 at 15:10 Comment(0)
S
1

It's by definition. It's a string and it is handled as such. No matter the meaning of the string.

If your logic would be applied. Then how about following examples:

"1+3==2"
"humans have four eyes"

Are they also falsy?

Spinoza answered 26/9, 2015 at 14:58 Comment(0)
F
1

If you consider the Boolean values, false is false and true is true.

Boolean(true) //returns true
Boolean(false) //returns false

But when you are talking about the strings like 'true' and 'false' or any other non-empty string, Javascript will not read them as Booleans and they will be true as all other non-empty strings.

Boolean('true') //returns true
Boolean('false') //returns true
Boolean('blahblahblah') //returns true
Fargone answered 8/4, 2021 at 1:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.