# If then else, elseif

**URL:** <https://discourse.nodered.org/t/if-then-else-elseif/80692>\
**Category:** General\
**Created:** [21 August 2023 15:06 UTC](https://discourse.nodered.org/t/if-then-else-elseif/80692 "2023-08-21T15:06:13Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![CJ10](https://avatars.discourse-cdn.com/v4/letter/c/f6c823/32.png) [@CJ10](https://discourse.nodered.org/u/CJ10)\
**Post date:** [21 August 2023 15:06 UTC](https://discourse.nodered.org/t/if-then-else-elseif/80692/1 "2023-08-21T15:06:13Z")

</div>

I am trying to use three x if statements, without an else.  
I seem to be struggling though. I think I probably ought to be using elseif but not sure how.  
My 2xflow variables are set to 0 in a separate function node.  
Can anyone offer assitance?

```auto
//declare state and count variable
var state = flow.get("redstate21");
var count = flow.get("redcount21");

//check to see that state has not changed
If(msg.payload = state)
{
    msg.payload = count;
}

//if payload is 0 and state was 1, do nothing 
   If(msg.payload < state)
    flow.set('redstate21', 0)
{
    msg.payload = count;
}

//if payload is 1 but state was 0, increase count by 1
 If(msg.payload > state)
 { 
    //increase the count by 1
    msg.payload = count+1
    //increase the flow,redcount21 variable by 1
    flow.set('redcount21', count);
    // record current state
    flow.set('redstate21', 1)
 }
 return msg;

```

---

<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:** [21 August 2023 15:11 UTC](https://discourse.nodered.org/t/if-then-else-elseif/80692/2 "2023-08-21T15:11:49Z")

</div>

Replace "If" with "if" everywhere.

if (msg.payload = state) makes payload equal to state.  
The test for equality is ==

Because you are not specifying a default value for state and count, if those variables do not exist, you may get unexpected results.

try  
`let count = flow.get("redcount21") ?? 0`

---

<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:** [21 August 2023 15:27 UTC](https://discourse.nodered.org/t/if-then-else-elseif/80692/3 "2023-08-21T15:27:55Z")

</div>

> [@CJ10](#):
>
> I think I probably ought to be using elseif but not sure how.

Javascript does not have elsif, just use else and if together

```auto
if (msg.payload == state) {
  ...
} else if (msg.payload < state) {
  ...
} else {
  ...
}

```

---

<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:** [21 August 2023 15:31 UTC](https://discourse.nodered.org/t/if-then-else-elseif/80692/4 "2023-08-21T15:31:36Z")

</div>

> [@jbudd](#):
>
> let count = flow.get("redcount21") ?? 0

I think that syntax only works with later versions of nodejs. Possible 18+ I am not sure. If it doesn't then  
`let count = flow.get("redcount21") || 0`  
will do the job.

---

<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:** [21 August 2023 16:30 UTC](https://discourse.nodered.org/t/if-then-else-elseif/80692/5 "2023-08-21T16:30:36Z")

</div>

` msg.payload = count;`

This will create a reference problem as you are overwriting `msg.payload`

Perhaps it is easier to describe what you want your flow to do and what the input is and the expected output.

---

<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:** [21 August 2023 16:32 UTC](https://discourse.nodered.org/t/if-then-else-elseif/80692/6 "2023-08-21T16:32:40Z")

</div>

The [nullish coalescing operator was introduced in ES2020](https://2ality.com/2019/08/nullish-coalescing.html), the nullish coalescing operator isn't supported in Node.js 12 and below.

---

<div class="post-metadata">

**Author:** ![CJ10](https://avatars.discourse-cdn.com/v4/letter/c/f6c823/32.png) [@CJ10](https://discourse.nodered.org/u/CJ10)\
**Post date:** [21 August 2023 19:38 UTC](https://discourse.nodered.org/t/if-then-else-elseif/80692/8 "2023-08-21T19:38:01Z")

</div>

I have a node which only outputs either a 1 or 0. And I would like to do a cumulative count every time that node switches from a 0 to 1 output but not if it changes from a 1 to 0.

---

<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:** [21 August 2023 19:55 UTC](https://discourse.nodered.org/t/if-then-else-elseif/80692/9 "2023-08-21T19:55:52Z")

</div>

Look at the RBE / Filter node. It permits data to pass when it changes.  
Therefore if you pass this through an RBE it will only permit the `msg` to pass when it is different to the last msg. Then you can test its value to see if it is 1.

`node that outputs 1 or 0` 👉 `RBE node`👉 `Switch node (test value is 1)`👉`counter node`.

There are lots of counter nodes in the palette manager or if your Node-RED is new enough, the debug node can be used as a counter.

Demo  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/6/a67dac915a6176ec029f5f67aaa4990858c6a290.png)

Demo Flow (use `CTRL+I` to import)

```auto
[{"id":"a4466d60c511231a","type":"inject","z":"FFF0000000000001","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":310,"y":460,"wires":[["3650cccef22fb776"]]},{"id":"9bc308a84c877216","type":"inject","z":"FFF0000000000001","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":310,"y":500,"wires":[["3650cccef22fb776"]]},{"id":"3650cccef22fb776","type":"rbe","z":"FFF0000000000001","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":false,"property":"payload","topi":"topic","x":450,"y":480,"wires":[["0dec3153bc4c3759"]]},{"id":"0dec3153bc4c3759","type":"switch","z":"FFF0000000000001","name":"payload == 1?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":610,"y":480,"wires":[["41e04ad8caeb0a61"]]},{"id":"41e04ad8caeb0a61","type":"debug","z":"FFF0000000000001","name":"counter","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"","statusType":"counter","x":600,"y":540,"wires":[]}]

```

---

<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:** [21 August 2023 21:04 UTC](https://discourse.nodered.org/t/if-then-else-elseif/80692/10 "2023-08-21T21:04:44Z")

</div>

> [@E1cid](#):
>
> the nullish coalescing operator isn't supported in Node.js 12 and below.

Wow, time passes so quickly.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [21 August 2023 21:47 UTC](https://discourse.nodered.org/t/if-then-else-elseif/80692/11 "2023-08-21T21:47:24Z")

</div>

> [@jbudd](#):
>
> The test for equality is ==

Not really. The test for equality is `===`. 3 `=` not 2. Using 2 `=` is "sort of equal" and should only be used when you really need it. Typically if your input might be an actual number or a number as a string. Otherwise it can easily introduce very hard to identify bugs and edge cases.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [21 August 2023 21:50 UTC](https://discourse.nodered.org/t/if-then-else-elseif/80692/12 "2023-08-21T21:50:19Z")

</div>

> [@Colin](#):
>
> > [@E1cid](#):
> >
> > the nullish coalescing operator isn't supported in Node.js 12 and below.
> 
> Wow, time passes so quickly.

How true. Especially as node.js v16 is about to pass into the long goodnight. It expires in September.

Unfortunately, Node-RED has fallen somewhat behind.

There are some very interesting changes coming to Node.js soon, such as these:

- v14 - optional chaining, Nullish Coalescing operators, String.prototype.matchAll,  
Intl.DisplayNames, Intl.DateTimeFormat, (Experimental: Async Local Storage, Top-Level Await, Diagnostic report), WeakReferences, private class methods
- v15 - logical assignment operators, String.prototype.replaceAll, Promise.any, AggregateError, AbortController, (Experimental Promisified setTimeout/setImmediate)
- v16 - Promisified setTimeout/setImmediate, RegExp match indices. npm v7 (peer deps now installed again). fs.rmdir no longer supports recursive (use new fs.rm)
- v17 - OpenSSL 3 (incl QUIC), Readline Promise API
- v18 - (Experimental: Fetch API, Web Streams API, Test Runner), HTTP Timeouts, findLast/findLastIndex array methods, Improvements to the Intl.Locale API [calendars, collations, hourCycles, numberingSystems, timeZones, textInfo, weekInfo], Intl.supportedValuesOf function

---

<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:** [21 August 2023 22:00 UTC](https://discourse.nodered.org/t/if-then-else-elseif/80692/13 "2023-08-21T22:00:09Z")

</div>

Dammit did i miss out an equals?

These shaky old digits cannot be relied on to oscilate at a reliable frequency! 🙃

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [21 August 2023 22:01 UTC](https://discourse.nodered.org/t/if-then-else-elseif/80692/14 "2023-08-21T22:01:35Z")

</div>

I usually blame the keyboard, the digits are fine!

---

<div class="post-metadata">

**Author:** ![CJ10](https://avatars.discourse-cdn.com/v4/letter/c/f6c823/32.png) [@CJ10](https://discourse.nodered.org/u/CJ10)\
**Post date:** [22 August 2023 07:59 UTC](https://discourse.nodered.org/t/if-then-else-elseif/80692/15 "2023-08-22T07:59:00Z")

</div>

Yes, you’re right

---

<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:** [5 September 2023 07:59 UTC](https://discourse.nodered.org/t/if-then-else-elseif/80692/16 "2023-09-05T07:59:08Z")

</div>

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