# 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:** 9
**Page:** 2

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

</div>

Now it should be ok...

---

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

</div>

I went through and formatted the lines as I know.

Aren't there `}` missing in these places?

 ![Screenshot from 2023-03-20 19-52-51](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/f/bf1f686de7582dad63e6a44ad82231350bb4375a.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:56 UTC](https://discourse.nodered.org/t/how-to-check-types-with-if-in-function-node/76723/23 "2023-03-20T08:56:46Z")

</div>

Yes, there are.  
I've already corrected the code in the post above... for your reference.

---

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

</div>

Sorry.

I'm tripping over the replies.

I'll copy/paste the new version.

---

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

</div>

> [@jbudd](#):
>
> ```auto
> let a = parseInt(check[1]);
> let type = typeof a
> node.warn(`typeof ${a} is ${type}`);
> 
> ```

I hope I am not caught up with things.

Please, I am asking because I don't know, so don't get upset.

I get the first and second line.

But this _magic_ in the third line.

${a} and ${type}

Oh.... Ok.  
So rather than _complicating_ the line - as I would write it to be:  
`node.warn("variable " + a + " is " + type);`  
you've written it as you did.  
The `${a}` and `${type}` are .... \*pointers (?) to the contents of the variables and so prints their contents in (between) the ` characters.  
Why did you use those and not the " or ' characters?  
Are the _advantages_ to using them?

---

<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 09:30 UTC](https://discourse.nodered.org/t/how-to-check-types-with-if-in-function-node/76723/26 "2023-03-20T09:30:57Z")

</div>

> [@Trying\_to\_learn](#):
>
> prints their contents in (between) the ` characters.  
> Why did you use those and not the " or ' characters?

The backticks are a relatively new feature in Javascript to simplify concatenating strings and variables.  
As you already realised

```auto
node.warn(`typeof ${a} is ${type}`)

```

is the same as  
`node.warn("typeof " + a + " is " + type)`

> [@Trying\_to\_learn](#):
>
> Are the _advantages_ to using them?

For me yes.  
I can never remember if I need to use + or & or , to concatenate strings and values in a node.warn statement.  
It's also familiar, now deprecated, syntax for a vaguely similar construction in Unix shell scripts.

I only recently read about the syntax ("template literals") in a page linked to by someone in your thread "Function nodes and 'let' for variables."

---

<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: [11 April 2023 20:49 UTC](https://discourse.nodered.org/t/how-to-check-types-with-if-in-function-node/76723/27 "2023-04-11T20:49:46Z")

</div>

Sorry for the long delay in posting and not following things up.

I just discovered a slight _problem_ with the node/code.

The idea is that the code checks for the required structure:  
`rgb,0,1,2,3` (For example.)

But if I send `rgb,o,1,2,3` it also passes.

My initial test was to check **missing** values.  
And now - ironically - even that doesn't seem to be working.

Code:

```auto
[{"id":"5204cbf368ea0df2","type":"function","z":"d188b95f33e5f7e4","name":"message format check","func":"// Split the payload.\nconst check = msg.payload.split(\",\");\nlet error = false;\n\nfor (let i = 0, l = check.length; i < l; i++) {\n const c = check[i];\n //node.warn(\"Checking \" + c);\n switch (i) {\n case 0: {\n if (c !== \"rgb\") {\n error = true;\n break;\n }\n }\n case 1: {\n if (\"number\" !== typeof c && c < 0 && c > 8) { // you're sure it's '8' , not '7' ? \n error = true;\n break;\n }\n }\n default: {\n if (\"number\" !== typeof c && c < 0 && c > 255) {\n error = true;\n break;\n }\n }\n }\n}\n\nif (error) {\n //\n node.warn(\"There's a problem...\");\n return [null,msg];\n}\n\nreturn msg;","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1985,"y":580,"wires":[["06770fdd22d032f8"],["ee97d5a02007f898"]],"outputLabels":["Good message","Bad message"],"l":false},{"id":"26c46db02b5ee25f","type":"inject","z":"d188b95f33e5f7e4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"rgb,0,1,2,3","payloadType":"str","x":1820,"y":510,"wires":[["5204cbf368ea0df2"]]},{"id":"5cc9b6bb2f0f5926","type":"inject","z":"d188b95f33e5f7e4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"rgb,o,1,2,3","payloadType":"str","x":1820,"y":540,"wires":[["5204cbf368ea0df2"]]},{"id":"482677f5d7bb2025","type":"inject","z":"d188b95f33e5f7e4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"rgb,0,i,2,3","payloadType":"str","x":1820,"y":570,"wires":[["5204cbf368ea0df2"]]},{"id":"185d0f8706cb36b3","type":"inject","z":"d188b95f33e5f7e4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"rgb,,1,Z,3","payloadType":"str","x":1820,"y":600,"wires":[["5204cbf368ea0df2"]]},{"id":"d95a1c38f04c4f1b","type":"inject","z":"d188b95f33e5f7e4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"rgb,0,1,2,E","payloadType":"str","x":1820,"y":630,"wires":[["5204cbf368ea0df2"]]},{"id":"6cbf789e3fcc449c","type":"inject","z":"d188b95f33e5f7e4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"rgb,,1,2,3","payloadType":"str","x":1820,"y":660,"wires":[["5204cbf368ea0df2"]]},{"id":"d61fdb93f7be6775","type":"inject","z":"d188b95f33e5f7e4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"rgb,0,,2,3","payloadType":"str","x":1820,"y":690,"wires":[["5204cbf368ea0df2"]]},{"id":"a47e39a43ffcf7d3","type":"inject","z":"d188b95f33e5f7e4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"rgb,0,1,,3","payloadType":"str","x":1820,"y":720,"wires":[["5204cbf368ea0df2"]]},{"id":"e3cffd78fb8efec8","type":"inject","z":"d188b95f33e5f7e4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"rgb,0,1,2,","payloadType":"str","x":1820,"y":750,"wires":[["5204cbf368ea0df2"]]},{"id":"06770fdd22d032f8","type":"debug","z":"d188b95f33e5f7e4","name":"GOOD","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2120,"y":540,"wires":[]},{"id":"ee97d5a02007f898","type":"debug","z":"d188b95f33e5f7e4","name":"WRONG","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2130,"y":610,"wires":[]}]

```

I'll do some more testing, but if I don't ask..... 😉

---

<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: [12 April 2023 00:18 UTC](https://discourse.nodered.org/t/how-to-check-types-with-if-in-function-node/76723/28 "2023-04-12T00:18:51Z")

</div>

In your function:

 ![Untitled 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/6/969cddd5a31c4d650f35507b9ad23a13c11c5829.jpeg)

Try this instead

```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") {
                node.warn(`Error in part ${i}: ${c} is invalid`)
                error = true;
            }
            break;
        }
        case 1: {
            let digit = parseInt(c) // gives either an integer or NaN
            if (!(digit >= 0 && digit <= 8)) { // you're sure it's '8' , not '7' ? 
                node.warn(`Error in part ${i}: ${c} is invalid`)
                error = true;  
            }
            break;
        }
        default: {
            let digit = parseInt(c)
            if (!(digit >= 0 && digit <= 255)) {
                node.warn(`Error in part ${i}: ${c} is invalid`)
                error = true;
            }
            break;
        }
    }
}

if (error) {
    //
    node.warn("There's a problem...");
    return [null,msg];
}

return msg;

```

---

<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: [12 April 2023 02:45 UTC](https://discourse.nodered.org/t/how-to-check-types-with-if-in-function-node/76723/29 "2023-04-12T02:45:02Z")

</div>

Thanks for that.

I just copied the code posted in reply 12.

I am not sure if it was simply the fact that I put the node/code in and it _frightened_ the errors away for a while. 😉

But _they_ regrouped and came back with a bit of vengeance.

I was a bit suspicious the original code wouldn't work, but as it seemed to fix the problem.

Anyway, _falling forwards_ I see what you posted makes more sense.  
I'll now go back and shake the tree again and see what happens. 🙂

Appreciated.

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