# Should assigning a value to a non-existent property give a run-time error?

**URL:** <https://discourse.nodered.org/t/should-assigning-a-value-to-a-non-existent-property-give-a-run-time-error/66768>\
**Category:** General\
**Created:** [22 August 2022 15:57 UTC](https://discourse.nodered.org/t/should-assigning-a-value-to-a-non-existent-property-give-a-run-time-error/66768 "2022-08-22T15:57:21Z")\
**Posts on this page:** 8\
**Page:** 1

<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:** [22 August 2022 15:57 UTC](https://discourse.nodered.org/t/should-assigning-a-value-to-a-non-existent-property-give-a-run-time-error/66768/1 "2022-08-22T15:57:21Z")

</div>

I expected this code (in a function node) to generate a run-time error, but it doesn't.

```auto
msg.payload = 0
msg.payload.x = 7
return msg;

```

Do others find this surprising?

node-red 3.0.2  
nodejs 16.17.0

---

<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:** [22 August 2022 16:02 UTC](https://discourse.nodered.org/t/should-assigning-a-value-to-a-non-existent-property-give-a-run-time-error/66768/2 "2022-08-22T16:02:28Z")

</div>

By default, JavaScript doesn't throw an error when you try to assign a property to a type like number, boolean or string. It silently lets it fail.

If you enable strict mode, (add `use strict` to the top of the function) it will throw an error. Although I havent checked if that works inside the Function node.

---

<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:** [22 August 2022 16:08 UTC](https://discourse.nodered.org/t/should-assigning-a-value-to-a-non-existent-property-give-a-run-time-error/66768/3 "2022-08-22T16:08:39Z")

</div>

OK, understood, thanks.

`use strict` throws a syntax error  
`"SyntaxError: Unexpected identifier (body:line 1)"`

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [23 August 2022 16:28 UTC](https://discourse.nodered.org/t/should-assigning-a-value-to-a-non-existent-property-give-a-run-time-error/66768/4 "2022-08-23T16:28:47Z")

</div>

Are you actually doing this inside a `function` node? I have not been able get one to honor the `"use strict"` directive. (The monaco editor complains, of course.) What might I be doing wrong?

---

<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:** [23 August 2022 17:01 UTC](https://discourse.nodered.org/t/should-assigning-a-value-to-a-non-existent-property-give-a-run-time-error/66768/5 "2022-08-23T17:01:12Z")

</div>

I meant that `use strict` throws the error.

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [23 August 2022 17:06 UTC](https://discourse.nodered.org/t/should-assigning-a-value-to-a-non-existent-property-give-a-run-time-error/66768/6 "2022-08-23T17:06:39Z")

</div>

Needs to be in inverted commas, I think?

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [24 August 2022 15:42 UTC](https://discourse.nodered.org/t/should-assigning-a-value-to-a-non-existent-property-give-a-run-time-error/66768/7 "2022-08-24T15:42:03Z")

</div>

```auto
'use strict'

msg.payload = 0
msg.payload.x = 7

return msg

```

Just returns `0` for me. As @drmibell says `'use strict'` in a function node is ignored. I have never noticed this before

---

<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:** [23 October 2022 15:42 UTC](https://discourse.nodered.org/t/should-assigning-a-value-to-a-non-existent-property-give-a-run-time-error/66768/8 "2022-10-23T15:42:24Z")

</div>

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