# How to compare and evaluate values in the function node

**URL:** <https://discourse.nodered.org/t/how-to-compare-and-evaluate-values-in-the-function-node/73054>\
**Category:** General\
**Tags:** function-node\
**Created:** [2 January 2023 09:33 UTC](https://discourse.nodered.org/t/how-to-compare-and-evaluate-values-in-the-function-node/73054 "2023-01-02T09:33:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dom1](https://avatars.discourse-cdn.com/v4/letter/d/a3d4f5/32.png) [@Dom1](https://discourse.nodered.org/u/Dom1)\
**Post date:** [2 January 2023 09:33 UTC](https://discourse.nodered.org/t/how-to-compare-and-evaluate-values-in-the-function-node/73054/1 "2023-01-02T09:33:11Z")

</div>

Hello Guys  
I'm stuck probably with a beginner problem.  
I want, if the Input "ns=4;s=Arp.Plc.Eclr/M\_HS\_Node\_Red\_1" is not equal "1" and the input "ns=4;s=Arp.Plc.Eclr/M\_HS\_Node\_Red\_Anf" is "true" then send out "true" else send out "false".

Here is my code:

```auto
if ( (msg.topic == "ns=4;s=Arp.Plc.Eclr/M_HS_Node_Red_1", msg.payload !=1) && (msg.topic == "ns=4;s=Arp.Plc.Eclr/M_HS_Node_Red_Anf", msg.payload = true))
{
        msg.payload = true;
        msg.payload = (msg.payload === "true") ? true : false;
        return (msg.topic = "ns=4;s=Arp.Plc.Eclr/M_HS_Node_Red_Anf1", msg.payload);
}
else
{ 
        msg.payload = false;
        msg.payload = (msg.payload === "false") ? true : false;
        return (msg.topic = "ns=4;s=Arp.Plc.Eclr/M_HS_Node_Red_Anf1", msg.payload);
}

```

But it doesnt work.  
I hope, anybody can help me.  
Thanks Dominic

---

<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:** [2 January 2023 10:00 UTC](https://discourse.nodered.org/t/how-to-compare-and-evaluate-values-in-the-function-node/73054/2 "2023-01-02T10:00:35Z")

</div>

Hmm, where to start...

  

> [@Dom1](#):
>
> `if ( (msg.topic == "ns=4;s=Arp.Plc.Eclr/M_HS_Node_Red_1", msg.payload !=1) && (msg.topic == "ns=4;s=Arp.Plc.Eclr/M_HS_Node_Red_Anf", msg.payload = true))`

- logical AND is `&&`
- logical OR is `||`

  

> [@Dom1](#):
>
> ```auto
> msg.payload = true;
> msg.payload = (msg.payload === "true") ? true : false;
> 
> ```

Setting `msg.payload = true` then checking to see if it EXACTLY matches "true" will always be false.

  

> [@Dom1](#):
>
> I want, if the Input "ns=4;s=Arp.Plc.Eclr/M\_HS\_Node\_Red\_1" is not equal "1" and the input "ns=4;s=Arp.Plc.Eclr/M\_HS\_Node\_Red\_Anf" is "true" then send out "true" else send out "false".

You cannot tests different variables if they are not inside the same message.

  

Lastly, as this is a low-code environment & what you are asking is pretty simple, there is no need to use a function node. Especially true since you do not understand the JavaScript syntax. In low code a set of "switch" and "change" nodes should be all you need.

* * *

If you want further help, you are going to have to show us what values to get (aka use debug nodes) and share your flow.

---

<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:** [3 March 2023 10:01 UTC](https://discourse.nodered.org/t/how-to-compare-and-evaluate-values-in-the-function-node/73054/3 "2023-03-03T10:01:03Z")

</div>

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