# What is wrong with my function? Javascript "if" doesn't seem to work

**URL:** https://discourse.nodered.org/t/what-is-wrong-with-my-function-javascript-if-doesnt-seem-to-work/39737
**Category:** General
**Created:** [25 January 2021 22:01 UTC](https://discourse.nodered.org/t/what-is-wrong-with-my-function-javascript-if-doesnt-seem-to-work/39737 "2021-01-25T22:01:21Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![jabss](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jabss/32/34422_2.png) [@jabss](https://discourse.nodered.org/u/jabss)
#### Post date: [25 January 2021 22:01 UTC](https://discourse.nodered.org/t/what-is-wrong-with-my-function-javascript-if-doesnt-seem-to-work/39737/1 "2021-01-25T22:01:21Z")

</div>

Hi,

I admit I'm not javascript knowlegeable neither programmer.... I'm cutting/pasting pieces of code in order to make things work...

I'm trying to get a flow working but I can't understand what I'm doing wrong on this one....  
Basically I'm using a function to perform some filtering based on "if" statements. While some of them work, there is one that is not working...

```auto
if(msg.device.ieeeAddr==="0x00124b00224350da") // This one works
{
 if(msg.payload.contact==="false") // This one never works
 {
  record_object="Kitchen Door Opened. Node 119. Contact value: " + msg.payload.contact;
  Timestamp = Timestamp=(new Date()).toISOString();
 }
 else // as alternative to the previous "if" statement, this one is always called.
 {
  record_object="Kitchen Door Closed. Node 119. Contact value: " + msg.payload.contact;
  Timestamp=Timestamp=(new Date()).toISOString();
 }
 record_string = Timestamp + "\n" + record_object;
 battery={
     Node: "119",
     Bat: msg.payload_raw.battery
     }
}

```

The function returns the message, which, by definition is wrong because if the contact value is "false", the message should be "Kitchen door Opened", not "Closed":

```auto
2021-01-25T21:42:56.578ZKitchen Door Closed. Node 119. Contact value: false

```

msg.payload:

```auto
{
"battery":100,
"battery_low":false,
"contact":false,
"linkquality":30,
"tamper":false,
"voltage":3100
}

```

msg.device:

```auto
{
"dateCode":"20191107",
"description":"Contact sensor",
"friendly_name":"SensorPortaCozinha",
"hardwareVersion":1,
"ieeeAddr":"0x00124b00224350da",
"lastSeen":1611610547344,
"manufacturerID":0,
"manufacturerName":"eWeLink",
"model":"SNZB-04",
"modelID":"DS01",
"networkAddress":4096,
"powerSource":"Battery",
"type":"EndDevice",
"vendor":"SONOFF",
"lastPayload":{"battery":100,
"battery_low":false,
"contact":false,
"linkquality":30,
"tamper":false,
"voltage":3100},
"homekit":{"ContactSensor":{"ContactSensorState":1},
"ContactSensor_Inverse":{"ContactSensorState":0},
"Battery":{"BatteryLevel":100,
"StatusLowBattery":0}}}

```

Any idea what am I doing wrong?  
Cheers

---

<div class="post-metadata">

### Author: ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)
#### Post date: [25 January 2021 22:11 UTC](https://discourse.nodered.org/t/what-is-wrong-with-my-function-javascript-if-doesnt-seem-to-work/39737/2 "2021-01-25T22:11:41Z")

</div>

> [@jabss](#):
>
> `if(msg.payload.contact==="false") // This one never works`

Hi @jabss,

I 'think' you should replace `if(msg.payload.contact==="false")` by `if(msg.payload.contact=="false")` or `if(msg.payload.contact===false)`.

Because `===` means _"equal and the same type"_. But in your input message the `"contact":false` means it is type _ **boolean** _, while `msg.payload.contact==="false"``means that you expect a _ **string** _ "false".

If you simply use `==`, it checks whether the value is equal but not the type.

P.S. I haven't tested my theory ...  
Bart

---

<div class="post-metadata">

### Author: ![jabss](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jabss/32/34422_2.png) [@jabss](https://discourse.nodered.org/u/jabss)
#### Post date: [25 January 2021 22:47 UTC](https://discourse.nodered.org/t/what-is-wrong-with-my-function-javascript-if-doesnt-seem-to-work/39737/3 "2021-01-25T22:47:25Z")

</div>

Hi,

I had tried before with the two equals " == " but without success....  
Haven't tried the true/false statement without the quotes ( " " ).  
With two equals and the statement without quotes it works!

```auto
if(msg.payload.contact==false)

```

Thank you very much!

---

<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: [26 March 2021 22:48 UTC](https://discourse.nodered.org/t/what-is-wrong-with-my-function-javascript-if-doesnt-seem-to-work/39737/4 "2021-03-26T22:48:18Z")

</div>

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