# Problem with syntax in function node for comparison

**URL:** <https://discourse.nodered.org/t/problem-with-syntax-in-function-node-for-comparison/10307>\
**Category:** General\
**Created:** [17 April 2019 22:08 UTC](https://discourse.nodered.org/t/problem-with-syntax-in-function-node-for-comparison/10307 "2019-04-17T22:08:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mike67](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@mike67](https://discourse.nodered.org/u/mike67)\
**Post date:** [17 April 2019 22:08 UTC](https://discourse.nodered.org/t/problem-with-syntax-in-function-node-for-comparison/10307/1 "2019-04-17T22:08:07Z")

</div>

Once more, I think i have a priblem with my syntax.  
I want to compare to things 🙂The msg.payload sent previously by an Inject node (boolean true or false) and a global variable xNR\_I1 wich can have the values "Active' or 'Inactive".

Then, according to the result, I set the msg.payload to "Entrée I activée" or "Entrée 1 désactivée".  
But the msg.payload always returns the input msg.payload.

I suppose my test in not written correctly, but even after several syntax tests, that still don't work.  
Here is my node 🙂

`[{"id":"360cf844.a58228","type":"function","z":"987ca6e6.534d28","name":"Test if Input 1 value has changed, and tweet it","func":"if\n((msg.payload === true) && (global.get(\"xNR_I1\") === \"Inactive\"))\n{msg.payload = \"Entree 1 Activée\"}\nelse\nif\n((msg.payload === false) && (global.get(\"xNR_I1\") === \"Active\"))\n{msg.payload = \"Entree 1 Désactivée\"}\nreturn msg;\n","outputs":1,"noerr":0,"x":1273.018798828125,"y":135.02500915527344,"wires":[["eda47117.c99aa","42fd53e8.195cbc"]]}]`

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [17 April 2019 23:14 UTC](https://discourse.nodered.org/t/problem-with-syntax-in-function-node-for-comparison/10307/2 "2019-04-17T23:14:36Z")

</div>

The problem is that the conditional statement is not covering all the possible combinations.

What do you want to assign to msg.payload when:

1- `msg.payload = true` and global variable = `Active` ?  
2- `msg.payload = false` and global variable = `Inactive` ?

---

<div class="post-metadata">

**Author:** ![mike67](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@mike67](https://discourse.nodered.org/u/mike67)\
**Post date:** [18 April 2019 05:49 UTC](https://discourse.nodered.org/t/problem-with-syntax-in-function-node-for-comparison/10307/3 "2019-04-18T05:49:23Z")

</div>

HI

In both cases you write, I just need nothing.

Those cases are not important for me, and so, I would like to exit the node without any result.

Envoyé de mon iPhone

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [18 April 2019 07:15 UTC](https://discourse.nodered.org/t/problem-with-syntax-in-function-node-for-comparison/10307/4 "2019-04-18T07:15:00Z")

</div>

Understood. There are a number of ways to code this algorithm. I would do like below.

Sometimes using the logical operator && can be confusing because of JavaScript short-circuit evaluation feature.

```auto
[{"id":"9e7fa9cb.268018","type":"tab","label":"Flow 6","disabled":false,"info":""},{"id":"46f941b9.06b7f","type":"function","z":"9e7fa9cb.268018","name":"Test if Input 1 value has changed, and tweet it","func":"let pay;\n\nlet gv = global.get(\"xNR_I1\");\nnode.status({text: gv});\n\n\nif (msg.payload === true) {\n if (gv === \"Inactive\") pay = \"Entree 1 Activée\"\n else pay = \"pas important\";\n}\n\nif (msg.payload === false) {\n if (gv === \"Active\") pay = \"Entree 1 Désactivée\"\n else pay = \"pas important\";\n}\n\nmsg.payload = pay;\n\nreturn msg;","outputs":1,"noerr":0,"x":460,"y":160,"wires":[["bb168d38.c13f8"]]},{"id":"621c8375.d1f21c","type":"inject","z":"9e7fa9cb.268018","name":"","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":160,"wires":[["46f941b9.06b7f"]]},{"id":"453436c4.5e9888","type":"inject","z":"9e7fa9cb.268018","name":"","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":220,"wires":[["46f941b9.06b7f"]]},{"id":"bb168d38.c13f8","type":"debug","z":"9e7fa9cb.268018","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":760,"y":160,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![mike67](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@mike67](https://discourse.nodered.org/u/mike67)\
**Post date:** [21 April 2019 20:36 UTC](https://discourse.nodered.org/t/problem-with-syntax-in-function-node-for-comparison/10307/5 "2019-04-21T20:36:36Z")

</div>

Thanks a lot.  
Your code works well. I thought I wrote the same thing, but it seems that the script syntax doens't accept my version. I have understood your code, but I haven't understood why mine doesn't work.  
Anyway, thanks a lot for your help.

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [21 April 2019 22:38 UTC](https://discourse.nodered.org/t/problem-with-syntax-in-function-node-for-comparison/10307/6 "2019-04-21T22:38:47Z")

</div>

> [@mike67](#):
>
> I haven't understood why mine doesn't work

Testing your code I verified that it will assign the expected strings to `msg.payload` in the following conditions:

You will get "Entree 1 Activee" when `msg.payload === true` AND `global variable === Inactive`. On the other hand, you will get "Entree 1 Desactivee" when `msg.payload === false` AND `global variable === Active`

In a couple of cases `msg.payload` will not receive a string from the code inside the function. Those are the cases that are not important.

Your code can behave like the one I proposed (as a matter of fact it will be even more robust) with a simple change depicted below. See line number 9.

 ![c-01](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/bb59423192d16daf9c708d22958c5407efb22a16.png)

---

<div class="post-metadata">

**Author:** ![mike67](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@mike67](https://discourse.nodered.org/u/mike67)\
**Post date:** [22 April 2019 12:38 UTC](https://discourse.nodered.org/t/problem-with-syntax-in-function-node-for-comparison/10307/7 "2019-04-22T12:38:53Z")

</div>

Thanks you for your great help.
