# Compare 2 variable

**URL:** <https://discourse.nodered.org/t/compare-2-variable/47568>\
**Category:** General\
**Created:** [24 June 2021 07:07 UTC](https://discourse.nodered.org/t/compare-2-variable/47568 "2021-06-24T07:07:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hydci](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hydci/32/43785_2.png) [@Hydci](https://discourse.nodered.org/u/Hydci)\
**Post date:** [24 June 2021 07:07 UTC](https://discourse.nodered.org/t/compare-2-variable/47568/1 "2021-06-24T07:07:39Z")

</div>

Good morning all,

I am brand new with node red, I managed to do several projects but I have a problem with this one I would like to compare 2 variables in IF if I put 2 == it does not work if I put 1 = it works but i get an "assignment in conditionel expression" warning i wish i knew what i'm doing wrong thanks for the help

```auto
var jourtest = new Date("2021-06-24");
var jourtest1 = new Date("2021-06-25");
var today = new Date ();

if (today === jourtest) {
    msg.payload = "test";
}
else if (today == jourtest1) {
    msg.payload = "test1";
}
else {
  msg.payload = "Non";
}
return msg;

```

---

<div class="post-metadata">

**Author:** ![Gunner](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gunner/32/43582_2.png) [@Gunner](https://discourse.nodered.org/u/Gunner)\
**Post date:** [24 June 2021 07:13 UTC](https://discourse.nodered.org/t/compare-2-variable/47568/2 "2021-06-24T07:13:31Z")

</div>

> [@Hydci](#):
>
> if I put 2 == it does not work if I put 1 = it works but i get an "assignment in conditionel expression" warning

Google says...

> **[What does "assignment in conditional expression" mean? | Codecademy](https://www.codecademy.com/forum_questions/52408e19f10c60a0e00042d8)**
>
> This is my code from lines 10 to 20; on lines 11, 13, and 14 it says "assignment in conditional expression"
> When I try the code is says "Oops, try again! Your code returned 'The result is a tie!' instead of 'paper wins' when the inputs are rock and...

Basically means you are assigning = when you should be comparing == _or_ ===

---

<div class="post-metadata">

**Author:** ![Hydci](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hydci/32/43785_2.png) [@Hydci](https://discourse.nodered.org/u/Hydci)\
**Post date:** [24 June 2021 07:27 UTC](https://discourse.nodered.org/t/compare-2-variable/47568/3 "2021-06-24T07:27:43Z")

</div>

Thank you for your answer, I looked at your link and made tests unfortunately being a beginner it and difficult to understand would you guide me on the mistake I made

---

<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:** [24 June 2021 07:38 UTC](https://discourse.nodered.org/t/compare-2-variable/47568/4 "2021-06-24T07:38:06Z")

</div>

hi @Hydci , welcome to node-red forum.

> [@Hydci](#):
>
> I looked at your link and made tests unfortunately being a beginner it and difficult to understand would you guide me on the mistake I made

1 `=` means **assign** value to variable  
2 `==` means **compare** 2 values  
3 `===` means strict **compare** 2 values

```auto
var x = 1 //assign 1 to x

//x is definitely 1 - lets check
console.log(x); //1 - yep it is 1

if ( x = 2 ) {
  console.log("you should not see this message but you will - why?")
}

//lets check x again...
console.log(x); //2 - oh no, it is now 2!

```

The answer is because inside the `if ( ... )` you assigned `2` to `x`

* * *

PS: this is really not a node-red issue - I would recommend finding some basic online JavaScript training if you are going to be writing functions. there are lots of free training articles around the net.

---

<div class="post-metadata">

**Author:** ![watwat](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/watwat/32/43131_2.png) [@watwat](https://discourse.nodered.org/u/watwat)\
**Post date:** [24 June 2021 07:43 UTC](https://discourse.nodered.org/t/compare-2-variable/47568/5 "2021-06-24T07:43:18Z")

</div>

Try watching what values you get in the output by setting it to the variables.

```auto
msg.payload = today ==> "Thu Jun 24 2021 09:40:22 GMT+0200 (Central European Summer Time)"
msg.payload = jourtest ==> "Thu Jun 24 2021 02:00:00 GMT+0200 (Central European Summer Time)"

```

As you can see, the string is not the same (time is different) and therefore we end up in the else statement. The way to solve this is to format the dates so they only gives you the specific data.

---

<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:** [23 August 2021 07:43 UTC](https://discourse.nodered.org/t/compare-2-variable/47568/6 "2021-08-23T07:43:39Z")

</div>

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