Convert truthy or falsy to an explicit boolean, i.e. to True or False

97,923

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
Share:
97,923

Related videos on Youtube

Aracthor
Author by

Aracthor

Updated on January 17, 2021

Comments

  • Aracthor
    Aracthor over 3 years

    I have a variable. Let's call it toto.

    This toto can be set to undefined, 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 neither undefined nor null, 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 to false if toto is undefined or null and true 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
      Sam almost 9 years
      This 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
      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
    Sterling Bourne over 6 years
    Of note, Boolean("false") is true, when you probably want it to be false.
  • Danon
    Danon over 6 years
    Why on earth would you like string "false" to be falsy? Even php doesn't do that :D
  • RBT
    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 returns false. In what context will it return true?
  • Robo Robok
    Robo Robok over 6 years
    He means Boolean("false").
  • Sterling Bourne
    Sterling Bourne over 6 years
    @RBT - What does Boolean("false") return?
  • Robo Robok
    Robo Robok over 6 years
    @NoahDavid It returns true. The only string casted to false is an empty string ("").
  • Sterling Bourne
    Sterling Bourne over 6 years
    Exactly. 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
    Robo Robok about 5 years
    What are you talking about?
  • Danish
    Danish about 5 years
    yup true Boolean("false") return true lol use this Boolean(JSON.parse("false"))
  • Brandon
    Brandon over 4 years
    The 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

Related