# How to check \*types\* with \`if\` in \`function\` node

**URL:** <https://discourse.nodered.org/t/how-to-check-types-with-if-in-function-node/76723>\
**Category:** General\
**Created:** [20 March 2023 04:13 UTC](https://discourse.nodered.org/t/how-to-check-types-with-if-in-function-node/76723 "2023-03-20T04:13:54Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [20 March 2023 04:13 UTC](https://discourse.nodered.org/t/how-to-check-types-with-if-in-function-node/76723/1 "2023-03-20T04:13:54Z")

</div>

I get how the `if()` works. (_well: I thought I did_)

```auto
if (x == 0)
{
    // do something here;
}
// continue as normal.

```

But say I want to check `x` is..... a number? (and is between 0 and 8)  
_(ok, that's just another `if` nested in there)_

How do I do that?  
(Check it is a number?)  
I know in the `switch` node there are options `of type`.....  
But I don't know the finer details of how that works.  
So I'm asking here, now.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [20 March 2023 04:45 UTC](https://discourse.nodered.org/t/how-to-check-types-with-if-in-function-node/76723/2 "2023-03-20T04:45:46Z")

</div>

This my best effort for now.

```auto
[{"id":"5204cbf368ea0df2","type":"function","z":"aaa025a7d2bb5b14","name":"message format check","func":"// Split the payload.\nlet check = msg.payload.split(\",\");\n//node.warn(check);\n\nlet error = 0;\nlet a = \"\";\n\n// Check RGB part of message.\nif (check[0] != \"rgb\")\n{\n // RGB wrong\n //node.warn(\"RGB part wrong\");\n error = 1;\n}\n\nif (error == 0)\n{\n a = parseInt(check[1]);\n //node.warn(\"A \" + a);\n if (isNaN(a))\n {\n // Part 1 problem.\n //node.warn(\"Part 1 problem.\");\n error = 1;\n }\n}\n\nif (error == 0)\n{\n a = parseInt(check[2]);\n //node.warn(\"A \" + a);\n if (isNaN(a)) {\n // Part 2 problem.\n //node.warn(\"Part 2 problem.\");\n error = 1;\n }\n}\n\nif (error == 0)\n{\n a = parseInt(check[3]);\n //node.warn(\"A \" + a);\n if (isNaN(a)) {\n // Part 3 problem.\n //node.warn(\"Part 3 problem.\");\n error = 1;\n }\n}\n\nif (error == 0) \n{\n a = parseInt(check[4]);\n //node.warn(\"A \" + a);\n if (isNaN(a)) {\n // Part 4 problem.\n //node.warn(\"Part 4 problem.\");\n error = 1;\n }\n}\n\nif (error == 0)\n{\n // All good\n return msg;\n}\nelse\n{\n node.warn(\"We have a problem\");\n return [null,msg];\n}\n","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":150,"wires":[[],[]],"outputLabels":["Good message","Bad message"]}]

```

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [20 March 2023 06:26 UTC](https://discourse.nodered.org/t/how-to-check-types-with-if-in-function-node/76723/3 "2023-03-20T06:26:50Z")

</div>

```auto
if ("number" === typeof x && x >= 0 && x <= 8)
{
    // do something here;
}
// continue as normal.

```

[typeof](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof) is your friend here...

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [20 March 2023 06:38 UTC](https://discourse.nodered.org/t/how-to-check-types-with-if-in-function-node/76723/4 "2023-03-20T06:38:59Z")

</div>

Sorry.

I was quick to mark it as the solution.

It **IS** the solution but I am not translating what you said to what I want.

Your `"number"`.....  
and `x` ~~and `y`.~~

I'll keep looking and see if I can work it out.

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [20 March 2023 06:47 UTC](https://discourse.nodered.org/t/how-to-check-types-with-if-in-function-node/76723/5 "2023-03-20T06:47:44Z")

</div>

`"number" === typeof x`: `typeof x` checks the type of `x` and returns a string. If it's a number (as you tried to test) it returns `"number"`.

`x>=0 && x<=8` checks if 0\<= x \<= 8 ... , to verify that `x`

> is between 0 and 8

You don't need another `if` but can just concatenate the various criterias with `&&`. JS will bail out when the first fails ( === `false`).

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [20 March 2023 06:51 UTC](https://discourse.nodered.org/t/how-to-check-types-with-if-in-function-node/76723/6 "2023-03-20T06:51:46Z")

</div>

Again: Thanks.

But somehow I am not seeing it.

I should have posted this bit and _we_ can work from there.

```auto
if (error == 0)
{
    a = parseInt(check[1]);
    //node.warn("A " + a);
    if (isNaN(a))
    {
        // Part 1 problem.
        //node.warn("Part 1 problem.");
        error = 1;
    }
}

```

Yes, there is a bit/lot missing compared to what yours does.

That is what I have - as a start point.

So I am not seeing how this fits into what I have.  
`if ("number" === typeof x && x >= 0 && x <= 8)`

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [20 March 2023 06:55 UTC](https://discourse.nodered.org/t/how-to-check-types-with-if-in-function-node/76723/7 "2023-03-20T06:55:37Z")

</div>

> [@ralphwetzel](#):
>
> [typeof](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof) is your friend here...

Yes, but I am not understanding it all as much as I would like.

I got stuck on/with `oftype` rather than `typeof`.  
After I found that, I looked but it didn't help me as much as I would like.

I got that code I posted but ......

Anyway.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [20 March 2023 07:18 UTC](https://discourse.nodered.org/t/how-to-check-types-with-if-in-function-node/76723/8 "2023-03-20T07:18:45Z")

</div>

Pasting the important part of the code:

```auto
// Split the payload.
let check = msg.payload.split(",");
//node.warn(check);

let error = 0;
let a = "";

if (error == 0)
{
    a = parseInt(check[1]);
    node.warn("A " + a);
    if ("number" === typeof a)
    {
        // Part 1 problem.
        node.warn("Part 1 problem.");
        error = 1;
    }
}

```

Given I am sending it `rgb,0,0,0,0`  
Why do I get an error?

 ![Screenshot from 2023-03-20 18-19-43](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/8/48a662b15acac478f61df04676171d41226a162e.png)

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [20 March 2023 08:09 UTC](https://discourse.nodered.org/t/how-to-check-types-with-if-in-function-node/76723/9 "2023-03-20T08:09:04Z")

</div>

> [@Trying\_to\_learn](#):
>
> Why do I get an error?

You don't get an error; you get your expected result.

Remember your first question?

> [@Trying\_to\_learn](#):
>
> But say I want to check `x` is..... a number?

That's what this line does; now it's `a` but `x` ... but that's the only difference:

> [@Trying\_to\_learn](#):
>
> ` if ("number" === typeof a)`

So you successfully tested for a number; in the next lines you yet emit a `.warn` that says you have a `Part 1 problem`.

Guessing now: If you intend to assure, that `a` is **NOT** a number, do the test like

` if ("number" !== typeof a)`

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [20 March 2023 08:14 UTC](https://discourse.nodered.org/t/how-to-check-types-with-if-in-function-node/76723/10 "2023-03-20T08:14:01Z")

</div>

My head is spinning.

Not from what you showed me. It just likes to spin.

There are 4 parts to parse/scan.  
0 has to be `rgb,` - which is easy to test.  
1 - has to be a NUMBER from `0` to `8`. The code has to catch if NOT meeting that.  
(Sorry, my bad. (maybe)..... But I had posted my effort and that is how the main `function` node works.)  
2 - as 1, but form `0` to `255`  
3 and 4 like 2.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [20 March 2023 08:22 UTC](https://discourse.nodered.org/t/how-to-check-types-with-if-in-function-node/76723/11 "2023-03-20T08:22:43Z")

</div>

After your `let a = parseInt(check[1]);` a will either be a valid number or else NaN  
But there is no value in testing `if (typeof a === "number")` because typeof NaN is still "number"  
What you can do is test if a is between 0 and 256:

```auto
// Split the payload.
let check = msg.payload.split(",");
//node.warn(check);

let a = parseInt(check[1]);
let type = typeof a
node.warn(`typeof ${a} is ${type}`);
if (a >= 0 && a<256) {
    node.warn (`${a} is a valid number`)
}
else {
    node.warn (`${a} is not a valid number`)
}

return msg

```

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [20 March 2023 08:24 UTC](https://discourse.nodered.org/t/how-to-check-types-with-if-in-function-node/76723/12 "2023-03-20T08:24:38Z")

</div>

Perhaps like this...

```auto
// Split the payload.
const check = msg.payload.split(",");
let error = false;

for (let i=0, l=check.length; i<l; i++) {
    const c = check[i];
    switch (i) {
        case 0: {
            if (c !== "rgb") {
                error = true;
                break;
            }
        }
        case 1: {
            if ("number" !== typeof c && c < 0 && c > 8) { // you're sure it's '8' , not '7' ? 
                error = true;
                break;
            }
        }
        default: {
            if ("number" !== typeof c && c < 0 && c > 255) {
                error = true;
                break;
            }
        }
    }
}

if (error) {
    node.warn("There's a problem...");
}

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [20 March 2023 08:25 UTC](https://discourse.nodered.org/t/how-to-check-types-with-if-in-function-node/76723/13 "2023-03-20T08:25:22Z")

</div>

SORRY... (I seem to say that a lot here)

I have (for now) left out the range testing.

I just want to get a part working then make it more complicated. 😉

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [20 March 2023 08:28 UTC](https://discourse.nodered.org/t/how-to-check-types-with-if-in-function-node/76723/14 "2023-03-20T08:28:47Z")

</div>

Thanks for that.

The `8` is because `0` to `7` are the individual LEDs.  
`8` is ALL of them at once. 😉

{{Thanks}}

I also _(stubbornly)_ used `1` and `0` for the error only because I am a dinosaur and didn't think to use that style.

I'll keep looking at what you posted.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [20 March 2023 08:30 UTC](https://discourse.nodered.org/t/how-to-check-types-with-if-in-function-node/76723/15 "2023-03-20T08:30:57Z")

</div>

The point I am trying to make is that `let a = parseInt(check[1])` **forces** typeof a to be "number" **even if it is not a valid number** (NaN)  
At least that's how I understand it.

Did you try the code I posted?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [20 March 2023 08:33 UTC](https://discourse.nodered.org/t/how-to-check-types-with-if-in-function-node/76723/16 "2023-03-20T08:33:58Z")

</div>

Not just now.

As you can see I am a bit busy.

I am about to look at it very soon.

---

<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 March 2023 08:36 UTC](https://discourse.nodered.org/t/how-to-check-types-with-if-in-function-node/76723/17 "2023-03-20T08:36:59Z")

</div>

> if ("number" === typeof x && x \>= 0 && x \<= 8)

Although that works, it can be confusing for people to grasp in concept if it is reversed like that.

```auto
if(typeof x === "number"){ ... }

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [20 March 2023 08:44 UTC](https://discourse.nodered.org/t/how-to-check-types-with-if-in-function-node/76723/18 "2023-03-20T08:44:14Z")

</div>

Um,

I copy/paste that code as given to my `function` node and it throws up errors.

`l` is not defined.

and down the bottom, it _seems_ that the the `{ }` aren't closed correctly.

Alas I am not used the the `{` being appended to the previous line.  
I'll load it into another editor that shows me formatting and see what it says.

 ![Screenshot from 2023-03-20 19-46-09](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/f/3fe025b54bd4929cc14aa1fccfb4b04c35958bb1.png)

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [20 March 2023 08:47 UTC](https://discourse.nodered.org/t/how-to-check-types-with-if-in-function-node/76723/19 "2023-03-20T08:47:23Z")

</div>

> [@ralphwetzel](#):
>
> `for (let i=0, l=check.length; i<l; i++) {`

Sorry ... there was a typo in this line... and some `}` missing.  
That happens when I write code directly into the editor here...

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [20 March 2023 08:50 UTC](https://discourse.nodered.org/t/how-to-check-types-with-if-in-function-node/76723/20 "2023-03-20T08:50:25Z")

</div>

There still seems to be an open `{ }` as the bottom line still ends with an indented `}` and no final closure to the last `}`

 ![Screenshot from 2023-03-20 19-49-59](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/d/0d199750e1e10c481df0c18911f794dc50471a0f.png)

[Next page](https://discourse.nodered.org/t/how-to-check-types-with-if-in-function-node/76723.md?page=2)
