# Trouble with javascript

**URL:** <https://discourse.nodered.org/t/trouble-with-javascript/26968>\
**Category:** General\
**Created:** [20 May 2020 06:40 UTC](https://discourse.nodered.org/t/trouble-with-javascript/26968 "2020-05-20T06:40:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thewaver](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thewaver/32/11917_2.png) [@Thewaver](https://discourse.nodered.org/u/Thewaver)\
**Post date:** [20 May 2020 06:40 UTC](https://discourse.nodered.org/t/trouble-with-javascript/26968/1 "2020-05-20T06:40:08Z")

</div>

Hi, trying to change msg.payload from true to on and false to off from the alexa node. Used a function node with the following but it doesnt work, can anyone tell me why.??

if (msg.payload == "true"){  
msg.payload = "on";  
}  
else if (msg.payload == "false"){  
msg.payload = "off";  
}  
return msg;

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [20 May 2020 06:46 UTC](https://discourse.nodered.org/t/trouble-with-javascript/26968/2 "2020-05-20T06:46:31Z")

</div>

if its a typeof boolean true and false you need to do the comparison without the `“”`,so:

```auto
if (msg.payload == true) {

```

otherwise your looking for the literal strings of true and false.  
You can see which it is when you connect a debug to the previous node and check in the debug tab for the format of this message property.  
Johannes  
Ps: and a happy forum anniversary 🎉

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [20 May 2020 07:30 UTC](https://discourse.nodered.org/t/trouble-with-javascript/26968/3 "2020-05-20T07:30:00Z")

</div>

or

```auto
msg.payload = (msg.payload == true) ? "on" : "off";

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [20 May 2020 13:01 UTC](https://discourse.nodered.org/t/trouble-with-javascript/26968/4 "2020-05-20T13:01:57Z")

</div>

or even

```auto
msg.payload = msg.payload ? "on" : "off"

```

You don't need to test msg.payload == true as it is itself true (or false).

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [20 May 2020 13:23 UTC](https://discourse.nodered.org/t/trouble-with-javascript/26968/5 "2020-05-20T13:23:11Z")

</div>

That all depends on whether you wish ON when the payload is {}.

---

<div class="post-metadata">

**Author:** ![Thewaver](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thewaver/32/11917_2.png) [@Thewaver](https://discourse.nodered.org/u/Thewaver)\
**Post date:** [20 May 2020 13:24 UTC](https://discourse.nodered.org/t/trouble-with-javascript/26968/6 "2020-05-20T13:24:05Z")

</div>

Problem solved. Thank you to everyone who helped, very much appreciated.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [20 May 2020 13:46 UTC](https://discourse.nodered.org/t/trouble-with-javascript/26968/7 "2020-05-20T13:46:29Z")

</div>

> [@E1cid](#):
>
> That all depends on whether you wish ON when the payload is {}

If you want to test for exactly `true` rather than just truthy then you should use `===`. With `==` you will see a yellow warning triangle in the editor.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [20 May 2020 13:51 UTC](https://discourse.nodered.org/t/trouble-with-javascript/26968/8 "2020-05-20T13:51:59Z")

</div>

As i said it depends what you require.

---

<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 June 2020 13:52 UTC](https://discourse.nodered.org/t/trouble-with-javascript/26968/9 "2020-06-03T13:52:00Z")

</div>

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