# Contains a sub string does not work

**URL:** <https://discourse.nodered.org/t/contains-a-sub-string-does-not-work/15621>\
**Category:** General\
**Created:** [17 September 2019 09:22 UTC](https://discourse.nodered.org/t/contains-a-sub-string-does-not-work/15621 "2019-09-17T09:22:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![WhiteLion](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/whitelion/32/12266_2.png) [@WhiteLion](https://discourse.nodered.org/u/WhiteLion)\
**Post date:** [17 September 2019 09:22 UTC](https://discourse.nodered.org/t/contains-a-sub-string-does-not-work/15621/1 "2019-09-17T09:22:36Z")

</div>

Hello,  
I ve this payload:  
"Relay Off 4C:11:AE:0D:4B:48"  
I want to use a function node to check if the string contains "On" or "Off" and this is my function which doesnt work. pls help:

```auto
if (msg.payload.contains == "Off")
{
    msg.payload = "Light Off";
    return msg;
}
if (msg.payload.contains == "On")
{
    msg.payload = "Light On";
    return msg;
}
return null;

```

I tried "msg.payload.contains("Off")" with double and single quotes ...  
Can someone help ? thx!

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [17 September 2019 09:28 UTC](https://discourse.nodered.org/t/contains-a-sub-string-does-not-work/15621/2 "2019-09-17T09:28:24Z")

</div>

In JavaScript there is no `contains` function available on the `String` object. Instead you can use `String.includes`:

```auto
if (msg.payload.includes("Off"))
{
   ...
}

```

MDN provides a useful reference of the available functions on Strings - [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global\_Objects/String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)

---

<div class="post-metadata">

**Author:** ![WhiteLion](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/whitelion/32/12266_2.png) [@WhiteLion](https://discourse.nodered.org/u/WhiteLion)\
**Post date:** [17 September 2019 09:34 UTC](https://discourse.nodered.org/t/contains-a-sub-string-does-not-work/15621/3 "2019-09-17T09:34:32Z")

</div>

Aha okay ! Thx ... in this case I am wondering why the "contains" keyword is accepted by the editor.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [17 September 2019 09:41 UTC](https://discourse.nodered.org/t/contains-a-sub-string-does-not-work/15621/4 "2019-09-17T09:41:29Z")

</div>

Because the editor isn't doing full type-aware syntax validation. It doesn't know what type of thing `msg.payload` is, so it doesn't know what properties exist.

As a line of code:

```auto
if (msg.payload.contains("Off")) {

```

is perfectly valid JavaScript syntax. `msg.payload` _might_ be an object that has a `contains` function. It is only when it runs the code will it know if that function exists or not.
