133

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.

polina-c
  • 6,245
  • 5
  • 25
  • 36
Aracthor
  • 5,757
  • 6
  • 31
  • 59
  • 10
    This question is marked as a duplicate but if you look at the other question http://stackoverflow.com/questions/263965/how-can-i-convert-a-string-to-boolean-in-javascript it is quite different; they are not duplicates at all – Sam Jul 01 '15 at 07:53
  • @Sam Sorry, but I confirmed myself this duplicate, I didn't know about this other question, but It did solve my problem. – Aracthor Jul 01 '15 at 07:54
  • 15
    @Aracthor It is NOT a duplicate, because http://stackoverflow.com/questions/263965/how-can-i-convert-a-string-to-boolean-in-javascript 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. – Alan McBee Nov 29 '17 at 18:09
  • On MDN, according to [Double NOT (!!)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_NOT#double_not_!!), a "truthy" conversion can be also be done through the [Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean/Boolean) function. – DavidRR Nov 09 '21 at 21:00

2 Answers2

197

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
Robo Robok
  • 21,132
  • 17
  • 68
  • 126
  • 81
    Of note, Boolean("false") is true, when you probably want it to be false. – Sterling Bourne Aug 10 '17 at 23:33
  • 59
    Why on earth would you like string "false" to be falsy? Even php doesn't do that :D – Danon Sep 11 '17 at 13:20
  • @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`? – RBT Sep 20 '17 at 00:18
  • 2
    He means `Boolean("false")`. – Robo Robok Sep 20 '17 at 07:39
  • @RBT - What does Boolean("false") return? – Sterling Bourne Sep 27 '17 at 19:27
  • 2
    @NoahDavid It returns true. The only string casted to `false` is an empty string (`""`). – Robo Robok Sep 27 '17 at 21:12
  • 7
    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. – Sterling Bourne Sep 28 '17 at 18:50
  • 3
    yup true `Boolean("false")` return true lol use this `Boolean(JSON.parse("false"))` – Danish Mar 27 '19 at 18:20
  • Beware! `var b = Boolean(false); if (b) { console.log('The Boolean object is not null nor undefined, thus passes the condition') }` – plesatejvlk Jul 15 '19 at 09:37
  • @plesatejvlk what browser? I checked on macOS (Chrome, Firefox, Safari), all of them didn't run the code inside `if()`. – Robo Robok Jul 15 '19 at 09:43
  • 1
    @RoboRobok My bad. It should have been "var b = new Boolean(false)" as per https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean – plesatejvlk Jul 15 '19 at 12:41
  • @SterlingBourne function bool(item){ return item === 'false' ? false : Boolean(item) } – DeltaTango Sep 14 '19 at 00:45
  • 3
    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 – Brandon Oct 28 '19 at 02:06
  • depending on your needs: `Boolean(String(v).toLowerCase() === "false" ? false : v)` – Nicholas Franceschina Apr 26 '21 at 20:19
  • Let's just summarize that if someone wants Boolean("false") to be false, something is seriously wrong in their design (unless, of course, he is parsing data, in which case the comment by @Danish stands). – Francesco Marchetti-Stasi Mar 07 '23 at 14:26
2

!!o is also shorthand of Boolean(o) and works exactly same. (for converting truthy/falsy to true/false).

let o = {a: 1}
Boolean(o) // true
!!o // true
// !!o is shorthand of Boolean(o) for converting `truthy/falsy` to `true/false`

Note that

yaya
  • 7,675
  • 1
  • 39
  • 38
  • Actually that doesn't work. Those will always return `true`, even for empty objects. Try: `Boolean({})` and `!!{}` – mayid Aug 09 '21 at 16:44
  • 2
    @mayid That depends on what you expect, reference types always have a value so `true` is expected. – A1rPun Mar 09 '22 at 11:20