# Function IF and set msg

**URL:** https://discourse.nodered.org/t/function-if-and-set-msg/41101
**Category:** General
**Created:** [16 February 2021 16:19 UTC](https://discourse.nodered.org/t/function-if-and-set-msg/41101 "2021-02-16T16:19:59Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Benja](https://avatars.discourse-cdn.com/v4/letter/b/fbc32d/32.png) [@Benja](https://discourse.nodered.org/u/Benja)
#### Post date: [16 February 2021 16:19 UTC](https://discourse.nodered.org/t/function-if-and-set-msg/41101/1 "2021-02-16T16:19:59Z")

</div>

![Captura](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/6/26f61d97fba6621139475cff132d81ebe1675611.jpeg)

Hi, I need help:  
I want to make a conditional that if the first msg is 0 the payload will go out through a specific exit, if it is 1 through another and so on, but I can't do the code.

sorry I'm a beginner

---

<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: [16 February 2021 16:24 UTC](https://discourse.nodered.org/t/function-if-and-set-msg/41101/2 "2021-02-16T16:24:14Z")

</div>

Can you paste code instead of screenshot (I can fix your code and explain why you have issues but I won't retype what you have already typed)

Clue: `let` has block scope meaning if you declare (`let`) a variable inside the if block, it won't be available outside of the if block.

---

<div class="post-metadata">

### Author: ![Benja](https://avatars.discourse-cdn.com/v4/letter/b/fbc32d/32.png) [@Benja](https://discourse.nodered.org/u/Benja)
#### Post date: [16 February 2021 16:25 UTC](https://discourse.nodered.org/t/function-if-and-set-msg/41101/3 "2021-02-16T16:25:20Z")

</div>

if (msg.payload[0] === '1');  
{let msg1 = msg.payload;  
return msg1;  
}  
else if (msg.payload[0] === '2');  
{let msg2 = msg.pyaload;  
return msg2;  
}else {};  
return msg1, msg2;

---

<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: [16 February 2021 16:35 UTC](https://discourse.nodered.org/t/function-if-and-set-msg/41101/4 "2021-02-16T16:35:18Z")

</div>

So, first things first. When posting code on the forum, please use the code button `</>` or paste code between backticks

`````  
`paste code like this`  
`````

* * *

Onto your issue...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/e/de4d55f8358574318e2e82ec53c862b866ac8aa2.png)

This is a better way...

```javascript
if (msg.payload[0] === '1') {
    return [msg, null, null]; //return msg to output 1
} else if (msg.payload[0] === '2') {
    return [null, msg, null]; //return msg to output 2
} else { 
    return [null, null, msg]; //return msg to output 3
}

```

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [16 February 2021 16:35 UTC](https://discourse.nodered.org/t/function-if-and-set-msg/41101/5 "2021-02-16T16:35:28Z")

</div>

Hi and welcome.

Your function would be more like this,.

```auto
if (msg.payload[0] === '1'){
    return [msg,null]; //send message output 1
}
else if (msg.payload[0] === '2'){
    return [null,msg]; // send message output 2
}
return null;

```

---

<div class="post-metadata">

### Author: ![Benja](https://avatars.discourse-cdn.com/v4/letter/b/fbc32d/32.png) [@Benja](https://discourse.nodered.org/u/Benja)
#### Post date: [16 February 2021 16:41 UTC](https://discourse.nodered.org/t/function-if-and-set-msg/41101/6 "2021-02-16T16:41:19Z")

</div>

thanks a lot

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [16 February 2021 17:24 UTC](https://discourse.nodered.org/t/function-if-and-set-msg/41101/7 "2021-02-16T17:24:38Z")

</div>

@Benja if you put your cursor on the red x’s you will get some information about the reason the `x` is there.

Something to file away for future reference,

---

<div class="post-metadata">

### Author: ![PaulKana](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paulkana/32/37106_2.png) [@PaulKana](https://discourse.nodered.org/u/PaulKana)
#### Post date: [17 February 2021 21:25 UTC](https://discourse.nodered.org/t/function-if-and-set-msg/41101/8 "2021-02-17T21:25:30Z")

</div>

Hi, is there a reason one should prefer a Function node to a Switch node? Is there a performance gain? 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: [17 February 2021 21:49 UTC](https://discourse.nodered.org/t/function-if-and-set-msg/41101/9 "2021-02-17T21:49:19Z")

</div>

A switch node is (marginally) more efficient than a function node, but more importantly it is easier to make simple mistakes and typos in a Function node than in a Change node, especially for those with limited js experience. You need to test any function node to make sure it works whereas you can assume that a change node will do what you tell it.  
Strictly of course a Function node will do what you tell it too, but I expect you know what I mean.

---

<div class="post-metadata">

### Author: ![PaulKana](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paulkana/32/37106_2.png) [@PaulKana](https://discourse.nodered.org/u/PaulKana)
#### Post date: [18 February 2021 10:35 UTC](https://discourse.nodered.org/t/function-if-and-set-msg/41101/10 "2021-02-18T10:35:57Z")

</div>

Thank you for the answer, I'll continue with Switch nodes then!

---

<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: [4 March 2021 10:36 UTC](https://discourse.nodered.org/t/function-if-and-set-msg/41101/11 "2021-03-04T10:36:01Z")

</div>

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