Function node Test whether a value is boolean or not

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

Just use a switch node ?

How about?

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

You can also use typeof to check if a value is a number. MDN is helpful here.

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.

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

1 Like

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.

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