# Function node Test whether a value is boolean or not

**URL:** <https://discourse.nodered.org/t/function-node-test-whether-a-value-is-boolean-or-not/71948>\
**Category:** General\
**Created:** [9 December 2022 22:17 UTC](https://discourse.nodered.org/t/function-node-test-whether-a-value-is-boolean-or-not/71948 "2022-12-09T22:17:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bodad](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bodad/32/22612_2.png) [@Bodad](https://discourse.nodered.org/u/Bodad)\
**Post date:** [9 December 2022 22:17 UTC](https://discourse.nodered.org/t/function-node-test-whether-a-value-is-boolean-or-not/71948/1 "2022-12-09T22:17:57Z")

</div>

It's getting late and I'm having a brain freeze.

I have a payload which is either boolean true or false. However, sometimes the request times out and an error code is returned as payload which is a negative integer. I'm trying to write a one line if clause in my function node to process the boolean value but not enter the clause if the value is non-integer.

I think  
if (msg.payload == true || msg.payload == false) // value is boolean  
{  
do some processing  
}  
// the evaluation of true or false failed and the processing did not happen

should work but is there a way of doing something like:  
if (msg.payload || !msg.payload) // value is boolean  
{  
do some processing  
}

to evaluate the payload as true or false?

Not explaining to well, but any help would be appreciated. Ta

---

<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:** [9 December 2022 22:24 UTC](https://discourse.nodered.org/t/function-node-test-whether-a-value-is-boolean-or-not/71948/2 "2022-12-09T22:24:57Z")

</div>

Just use a switch node ?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/5/4536760759be15eb54e768aa43aa583dedc24e9b.png)

---

<div class="post-metadata">

**Author:** ![phoddie](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/phoddie/32/64232_2.png) [@phoddie](https://discourse.nodered.org/u/phoddie)\
**Post date:** [9 December 2022 22:33 UTC](https://discourse.nodered.org/t/function-node-test-whether-a-value-is-boolean-or-not/71948/3 "2022-12-09T22:33:19Z")

</div>

> [@Bodad](#):
>
> if (msg.payload == true || msg.payload == false) // value is boolean

How about?

```auto
if ("boolean" === typeof msg.payload) { // value is boolean
}

```

You can also use `typeof` to check if a value is a number. MDN is [helpful](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof) here.

---

<div class="post-metadata">

**Author:** ![Bodad](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bodad/32/22612_2.png) [@Bodad](https://discourse.nodered.org/u/Bodad)\
**Post date:** [9 December 2022 22:41 UTC](https://discourse.nodered.org/t/function-node-test-whether-a-value-is-boolean-or-not/71948/4 "2022-12-09T22:41:49Z")

</div>

Thanks for the prompt reply. I'm trying to use a function node as there are quite a few things I need to process, but thanks anyway.

---

<div class="post-metadata">

**Author:** ![Bodad](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bodad/32/22612_2.png) [@Bodad](https://discourse.nodered.org/u/Bodad)\
**Post date:** [9 December 2022 22:42 UTC](https://discourse.nodered.org/t/function-node-test-whether-a-value-is-boolean-or-not/71948/5 "2022-12-09T22:42:24Z")

</div>

Code to test for Boolean or number. Perfect. Thanks.

---

<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:** [10 December 2022 09:40 UTC](https://discourse.nodered.org/t/function-node-test-whether-a-value-is-boolean-or-not/71948/6 "2022-12-10T09:40:33Z")

</div>

> [@Bodad](#):
>
> if (msg.payload == true || msg.payload == false)

That would have worked if you had used triple =  
`if (msg.payload === true || msg.payload === false)`  
That tells javascript to test for exactly those values. Otherwise it will do type conversion to test it.

---

<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:** [24 December 2022 09:41 UTC](https://discourse.nodered.org/t/function-node-test-whether-a-value-is-boolean-or-not/71948/7 "2022-12-24T09:41:29Z")

</div>

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