Convert truthy or falsy to an explicit boolean, i.e. to True or False
Solution 1
Yes, you can always use this:
var tata = Boolean(toto);
And here are some tests:
for (var value of [0, 1, -1, "0", "1", "cat", true, false, undefined, null]) {
console.log(`Boolean(${typeof value} ${value}) is ${Boolean(value)}`);
}
Results:
Boolean(number 0) is false
Boolean(number 1) is true
Boolean(number -1) is true
Boolean(string 0) is true
Boolean(string 1) is true
Boolean(string cat) is true
Boolean(boolean true) is true
Boolean(boolean false) is false
Boolean(undefined undefined) is false
Boolean(object null) is false
Solution 2
You can use Boolean(obj)
or !!obj
for converting truthy/falsy
to true/false
.
var obj = {a: 1}
var to_bool_way1 = Boolean(obj) // true
var to_bool_way2 = !!obj // true
Related videos on Youtube
Aracthor
Updated on January 17, 2021Comments
-
Aracthor over 3 years
I have a variable. Let's call it
toto
.This
toto
can be set toundefined
,null
, a string, or an object.I would like to check if
toto
is set to a data, which means set to a string or an object, and neitherundefined
nornull
, and set corresponding boolean value in another variable.I thought of the syntax
!!
, that would look like this:var tata = !!toto; // tata would be set to true or false, whatever toto is.
The first
!
would be set tofalse
if toto isundefined
ornull
andtrue
else, and the second one would invert it.But it looks a little bit odd. So is there a clearer way to do this?
I already looked at this question, but I want to set a value in a variable, not just check it in an
if
statement.-
Sam almost 9 yearsThis question is marked as a duplicate but if you look at the other question stackoverflow.com/questions/263965/… it is quite different; they are not duplicates at all
-
Alan McBee over 6 years@Aracthor It is NOT a duplicate, because stackoverflow.com/questions/263965/… is about converting string containing only the words "true" and "false" to their Boolean counterparts. This question is about converting ANY variable into a Boolean based on whether it is truthy or falsy.
-
-
Sterling Bourne over 6 yearsOf note, Boolean("false") is true, when you probably want it to be false.
-
Danon over 6 yearsWhy on earth would you like string "false" to be falsy? Even php doesn't do that :D
-
RBT over 6 years@NoahDavid I didn't get what you are trying to say. I simply ran the code
Boolean(false)
in developer tool console and it returnsfalse
. In what context will it returntrue
? -
Robo Robok over 6 yearsHe means
Boolean("false")
. -
Sterling Bourne over 6 years@RBT - What does Boolean("false") return?
-
Robo Robok over 6 years@NoahDavid It returns true. The only string casted to
false
is an empty string (""
). -
Sterling Bourne over 6 yearsExactly. Which is why you have to be careful when using the phrase "false" if it's in quotes and thus interpreted as a String. You may think you're casting it to false, but in fact it'll return true -- hence the original warning.
-
Robo Robok about 5 yearsWhat are you talking about?
-
Danish about 5 yearsyup true
Boolean("false")
return true lol use thisBoolean(JSON.parse("false"))
-
Brandon over 4 yearsThe string 'false' should never evaluate to falsy. There are important reasons to treat a string 'false' and boolean 'false' differently. Imagine if someone enters their name into your datebase as 'false'...do you want to print the name or start throwing errors? Automatically converting strings to different types based on their content is a dangerous game