# Convert Boolean to Integer

**URL:** https://discourse.nodered.org/t/convert-boolean-to-integer/31124
**Category:** General
**Created:** [4 August 2020 09:08 UTC](https://discourse.nodered.org/t/convert-boolean-to-integer/31124 "2020-08-04T09:08:15Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ikoth](https://avatars.discourse-cdn.com/v4/letter/i/ce73a5/32.png) [@ikoth](https://discourse.nodered.org/u/ikoth)
#### Post date: [4 August 2020 09:08 UTC](https://discourse.nodered.org/t/convert-boolean-to-integer/31124/1 "2020-08-04T09:08:15Z")

</div>

Sorry to trouble you with such a basic JavaScript problem, but this is driving me CRAZY.

I need to convert a Boolean true/false into a numeric value for further processing. The flow I'm creating takes the Boolean from a msg.payload array, and involves multiple "if" checks on different elements in the array, but to simplify things I've reproduced the bit that isn't working in the attached sample.

The objective is to simply return one integer when the Boolean is "true" and a different one when it's "false" but I just can't get it to work.

Thanks in advance,

I

`[{"id":"31261c3d.b8d2a4","type":"function","z":"e8716096.c220c","name":"","func":"if (msg.payload == \"true\") {\n msg.payload = 111;\n} else {\n msg.payload = 999;\n}\nreturn msg;","outputs":1,"noerr":0,"x":450,"y":200,"wires":[["e5e5b077.cc8d4"]]}]`

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [4 August 2020 09:12 UTC](https://discourse.nodered.org/t/convert-boolean-to-integer/31124/2 "2020-08-04T09:12:40Z")

</div>

So your function is ...

```auto
if (msg.payload == "true") {
 msg.payload = 111;
} else {
 msg.payload = 999;
}
return msg;

```

try...

```auto
node.warn(msg.payload)
msg.original = msg.payload;
if (msg.payload === "true" || msg.payload === true) {
 msg.payload = 111;
} else {
 msg.payload = 999;
}
return msg;

```

and show us the debug log.

---

<div class="post-metadata">

### Author: ![ikoth](https://avatars.discourse-cdn.com/v4/letter/i/ce73a5/32.png) [@ikoth](https://discourse.nodered.org/u/ikoth)
#### Post date: [4 August 2020 09:25 UTC](https://discourse.nodered.org/t/convert-boolean-to-integer/31124/3 "2020-08-04T09:25:11Z")

</div>

Many thanks for the prompt reply, and more importantly, thanks for fixing the function!

Your version works properly.

Much obliged.

---

<div class="post-metadata">

### Author: ![wb666greene](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wb666greene/32/6534_2.png) [@wb666greene](https://discourse.nodered.org/u/wb666greene)
#### Post date: [4 August 2020 17:57 UTC](https://discourse.nodered.org/t/convert-boolean-to-integer/31124/4 "2020-08-04T17:57:10Z")

</div>

You are not the first and certainly won't be the last to be tripped up by JavaScript == vs. ===

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [18 August 2020 17:57 UTC](https://discourse.nodered.org/t/convert-boolean-to-integer/31124/5 "2020-08-18T17:57:13Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
