== and === confusion

#1

if (msg.payload[i].Active== 0) this will test fine and everything is working as it should be.
However the editor in the function node gives me a warning Use '===' to compare with '0'.

So if I change that to if (msg.payload[i].Active=== 0) the editor warning is gone, but my function it not working anymore, this will always be tested falls no matter what the value is.

What do I miss or not understand?

0 Likes

#2

This is a good summary of the differences: https://stackoverflow.com/a/359509/2117239

In short, === is a strict type and value test. In your code, that means the Active property must be a Number type and must have the value 0.

The == comparison doesn't do strict type checking. So it will accept a much wider set of values. For example: "0"==0. But also false==0 and other possibly unexpected results. This is why you get a warning.

So the real question is what exactly is the value and type of the msg.payload[i].Active property. If you pass it to the debug sidebar, it should give you hint you need.

0 Likes

Need help with a simple function
#3

In this case msg.payload[i].Active is a string and can hold the value "0" or "1".

Thanks for the explanation and link, it's a clear now.

0 Likes

#4

In that case then possibly the test should be
if (msg.payload[i].Active === "0")

0 Likes