Compare 2 variable

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

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;

Google says...

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

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

hi @Hydci , welcome to node-red forum.

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

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.

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

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.

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