# Boolean true should equal string true (or better)

**URL:** <https://discourse.nodered.org/t/boolean-true-should-equal-string-true-or-better/12395>\
**Category:** General\
**Created:** [19 June 2019 12:30 UTC](https://discourse.nodered.org/t/boolean-true-should-equal-string-true-or-better/12395 "2019-06-19T12:30:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![michaelblight](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/michaelblight/32/445_2.png) [@michaelblight](https://discourse.nodered.org/u/michaelblight)\
**Post date:** [19 June 2019 12:30 UTC](https://discourse.nodered.org/t/boolean-true-should-equal-string-true-or-better/12395/1 "2019-06-19T12:30:03Z")

</div>

I sort of expect to be able to compare to true. So I code a switch node with "==" and look for a boolean type that I can set to true. I don't find one, so I reluctantly match with the string "true". My fairly complex flow doesn't work properly and it takes me wwaaaaayyyy too long to realise that boolean true != string true (I blame the red wine). So I Google it, and then realise "is true" and "is false" exist on the switch node. Doh! But really? How many people are going to encounter the same 'problem'? Surely most languages these days would coerce a boolean to a string and it would work.

Even better, all of this could be solved by leaving "is true" but also allow a boolean type in the comparison. I know I'm going to make the same mistake again and again. Please! Thoughts?

EDIT... And to fix up my potential mistakes in other nodes, why does searching (ctrl-F) for "true" seem to match with "on"?

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [20 June 2019 06:26 UTC](https://discourse.nodered.org/t/boolean-true-should-equal-string-true-or-better/12395/2 "2019-06-20T06:26:51Z")

</div>

Post your flow to give us some context to work with. Checking a boolean in javascript is fairly easy.

```auto
booleanValue = true

if( booleanValue === true ){
  do something
}

```

_edit_

with a switch node:

 ![15](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/a/a3ce2600c3053b44649b51a1a870f770a7ce0922.png)

_edit 2_ it's early, you figured it out.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [20 June 2019 07:31 UTC](https://discourse.nodered.org/t/boolean-true-should-equal-string-true-or-better/12395/3 "2019-06-20T07:31:26Z")

</div>

> [@michaelblight](#):
>
> Surely most languages these days would coerce a boolean to a string and it would work.

I don't think that is correct, certainly not javascript or C or Java. Can you give an example?

---

<div class="post-metadata">

**Author:** ![michaelblight](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/michaelblight/32/445_2.png) [@michaelblight](https://discourse.nodered.org/u/michaelblight)\
**Post date:** [20 June 2019 08:03 UTC](https://discourse.nodered.org/t/boolean-true-should-equal-string-true-or-better/12395/4 "2019-06-20T08:03:53Z")

</div>

Maybe coerce is a strong word. In C# you'd have to do `Boolean.TryParse("true")`, but it would work. Javascript has always been a bit crazy in many ways - `Boolean("false")` is true. Therefore you'd test if it was a string and handle "true". JSON (I'm pretty sure) parses "true" to true. I'm suggesting Node Red should do the same, purely for convenience and it's logical.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [20 June 2019 08:14 UTC](https://discourse.nodered.org/t/boolean-true-should-equal-string-true-or-better/12395/5 "2019-06-20T08:14:09Z")

</div>

Your C# example is converting a string to a boolean, and only at the explicit request of the programmer, not automatically performing the opposite conversion (boolean to string) without the explicit request of the programmer.  
In JSON everything is a string.

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [22 June 2019 14:05 UTC](https://discourse.nodered.org/t/boolean-true-should-equal-string-true-or-better/12395/6 "2019-06-22T14:05:36Z")

</div>

> [@michaelblight](#):
>
> Even better, all of this could be solved by leaving "is true" but also allow a boolean type in the comparison. I know I'm going to make the same mistake again and again. Please! Thoughts?

Haha, I never even noticed that there IS NO boolean selection on the `switch` node value pulldown... that does seem like it would be a reasonable thing to add -- of course, then getting the backend logic to _do the right thing_ would be the next challenge!

This i due to the fact that equality comparisons in Javascript (and therefore, in Node-RED, too) are at best confusing, and at worst just deceptive! Check out [Dorey's github site](https://dorey.github.io/JavaScript-Equality-Table/unified/) for a summary of all the "loose" (==) and "strict" (===) equality operators evaluate for all different data types...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/b094c2b64d592e4933098eac612d6a4b525e9ce5.png)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [22 June 2019 15:26 UTC](https://discourse.nodered.org/t/boolean-true-should-equal-string-true-or-better/12395/7 "2019-06-22T15:26:34Z")

</div>

> [@shrickus](#):
>
> Haha, I never even noticed that there IS NO boolean selection on the `switch` node value pulldown... that does seem like it would be a reasonable thing to add -- of course, then getting the backend logic to _do the right thing_ would be the next challenge!

Not in the `values` Look at the `rules` (tripped me up a number of times too)

 ![08%20AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/f/f12e47cd8cffb5fcc6c90a5e64e503642d5788c7.png)

---

<div class="post-metadata">

**Author:** ![michaelblight](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/michaelblight/32/445_2.png) [@michaelblight](https://discourse.nodered.org/u/michaelblight)\
**Post date:** [23 June 2019 00:33 UTC](https://discourse.nodered.org/t/boolean-true-should-equal-string-true-or-better/12395/8 "2019-06-23T00:33:54Z")

</div>

This is the point I'm trying to make - people are going to get tripped up all the time, and do as I did and use `= "true"`. My preference would be to (1) include Boolean as an option, but also (2) support the specific cases of "true" being true and "false" being false, since they're logical and make NR more accessible to the inexperienced. But don't get hung up on (2), when (1) is better anyway.

---

<div class="post-metadata">

**Author:** ![wasafiri](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wasafiri/32/49094_2.png) [@wasafiri](https://discourse.nodered.org/u/wasafiri)\
**Post date:** [6 October 2021 21:25 UTC](https://discourse.nodered.org/t/boolean-true-should-equal-string-true-or-better/12395/9 "2021-10-06T21:25:22Z")

</div>

Sorry to reply to an old topic. I had the same problem trying to figure out where to select the "boolean" option before I Googled for ten minutes and came upon this thread which helped me resolve the problem. The UX could stand to be fine tuned.
