# Null instead of undefined

**URL:** <https://discourse.nodered.org/t/null-instead-of-undefined/32490>\
**Category:** General\
**Created:** [6 September 2020 05:01 UTC](https://discourse.nodered.org/t/null-instead-of-undefined/32490 "2020-09-06T05:01:20Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daneev](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/daneev/32/10874_2.png) [@Daneev](https://discourse.nodered.org/u/Daneev)\
**Post date:** [6 September 2020 05:01 UTC](https://discourse.nodered.org/t/null-instead-of-undefined/32490/1 "2020-09-06T05:01:20Z")

</div>

Good day.

const set = [];  
set.push (undefined)  
msg.payload = [... set];  
return msg;

the result is null  
msg.payload: array [1]  
[null]  
Why not undefined?

The same code in the browser:  
const set = [];  
set.push (undefined)  
1  
[... set]  
[undefined]

Regards, Yuri.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [6 September 2020 08:34 UTC](https://discourse.nodered.org/t/null-instead-of-undefined/32490/2 "2020-09-06T08:34:09Z")

</div>

Hi Daneev welcome to the forum.

I'll assume ou are doing this in a `function` node, so could you copy and paste the full example of each case.

When you paste in the code please use the `</>` icon arround your code to preserve the formatting.

Thanks!

---

<div class="post-metadata">

**Author:** ![Daneev](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/daneev/32/10874_2.png) [@Daneev](https://discourse.nodered.org/u/Daneev)\
**Post date:** [6 September 2020 09:02 UTC](https://discourse.nodered.org/t/null-instead-of-undefined/32490/3 "2020-09-06T09:02:46Z")

</div>

```auto
[{"id":"4290dec2.c896d","type":"inject","z":"b51d87d0.c86408","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":310,"y":1420,"wires":[["ffef1f99.e3147"]]},{"id":"ffef1f99.e3147","type":"function","z":"b51d87d0.c86408","name":"","func":"var set = [];\nset.push(undefined);\nmsg.payload = [...set];\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":520,"y":1420,"wires":[["d9c498c9.9c4988"]]},{"id":"d9c498c9.9c4988","type":"debug","z":"b51d87d0.c86408","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":740,"y":1420,"wires":[]}]

```

---

<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:** [6 September 2020 09:04 UTC](https://discourse.nodered.org/t/null-instead-of-undefined/32490/4 "2020-09-06T09:04:15Z")

</div>

This appears to be a bug in the debug node output, which shows `null` for an array element that is actually `undefined`.

---

<div class="post-metadata">

**Author:** ![Daneev](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/daneev/32/10874_2.png) [@Daneev](https://discourse.nodered.org/u/Daneev)\
**Post date:** [6 September 2020 09:11 UTC](https://discourse.nodered.org/t/null-instead-of-undefined/32490/5 "2020-09-06T09:11:09Z")

</div>

Probably it is. But can this be fixed?

```auto
[{"id":"4290dec2.c896d","type":"inject","z":"b51d87d0.c86408","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":310,"y":1420,"wires":[["ffef1f99.e3147"]]},{"id":"ffef1f99.e3147","type":"function","z":"b51d87d0.c86408","name":"","func":"var set = new Set();\nset.add(undefined);\nmsg.payload = [...set];\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":520,"y":1420,"wires":[["d9c498c9.9c4988"]]},{"id":"d9c498c9.9c4988","type":"debug","z":"b51d87d0.c86408","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":740,"y":1420,"wires":[]}]

```

with sets the same way.

---

<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:** [6 September 2020 09:13 UTC](https://discourse.nodered.org/t/null-instead-of-undefined/32490/6 "2020-09-06T09:13:16Z")

</div>

What do you mean by fixed? It is only the debug output that is wrong. Just ignore it.

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [6 September 2020 09:13 UTC](https://discourse.nodered.org/t/null-instead-of-undefined/32490/7 "2020-09-06T09:13:23Z")

</div>

I would suggest to report it as an issue as it sounds like an obvious bug. You can report issues in the GitHub page: [https://github.com/node-red/node-red](https://github.com/node-red/node-red)

---

<div class="post-metadata">

**Author:** ![Daneev](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/daneev/32/10874_2.png) [@Daneev](https://discourse.nodered.org/u/Daneev)\
**Post date:** [6 September 2020 09:14 UTC](https://discourse.nodered.org/t/null-instead-of-undefined/32490/8 "2020-09-06T09:14:38Z")

</div>

These are different types of values. I can't help but pay attention.

---

<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:** [6 September 2020 09:19 UTC](https://discourse.nodered.org/t/null-instead-of-undefined/32490/9 "2020-09-06T09:19:23Z")

</div>

> [@Daneev](#):
>
> These are different types of values. I can't help but pay attention

Yes, but it does not affect the operation of the flow. As @ristomatti has suggested, file an issue and then just ignore the debug output (or disable that debug node).

---

<div class="post-metadata">

**Author:** ![Daneev](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/daneev/32/10874_2.png) [@Daneev](https://discourse.nodered.org/u/Daneev)\
**Post date:** [6 September 2020 09:26 UTC](https://discourse.nodered.org/t/null-instead-of-undefined/32490/10 "2020-09-06T09:26:47Z")

</div>

> <https://github.com/node-red/node-red/issues/2696>
>
> Good day.
> const set = \[\];
> set.push (undefined)
> msg.payload = \[... set\];
> return msg;
> the result is null
> msg.payload: array \[1\]
> \[null\]
> Why not undefined?
> The same code in the...

---

<div class="post-metadata">

**Author:** ![Daneev](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/daneev/32/10874_2.png) [@Daneev](https://discourse.nodered.org/u/Daneev)\
**Post date:** [6 September 2020 09:34 UTC](https://discourse.nodered.org/t/null-instead-of-undefined/32490/11 "2020-09-06T09:34:04Z")

</div>

```auto
[{"id":"4290dec2.c896d","type":"inject","z":"b51d87d0.c86408","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":1420,"wires":[["ffef1f99.e3147"]]},{"id":"ffef1f99.e3147","type":"function","z":"b51d87d0.c86408","name":"","func":"var set = new Set();\nset.add(undefined);\nmsg.payload = [...set];\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":400,"y":1420,"wires":[["a03fb66f.8b2a68"]]},{"id":"d9c498c9.9c4988","type":"debug","z":"b51d87d0.c86408","name":"yes","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":810,"y":1380,"wires":[]},{"id":"a03fb66f.8b2a68","type":"switch","z":"b51d87d0.c86408","name":"is of type undefined?","property":"payload","propertyType":"msg","rules":[{"t":"istype","v":"undefined","vt":"undefined"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":600,"y":1420,"wires":[["d9c498c9.9c4988"],["d0a30f32.cf1b3"]],"outputLabels":["yes","no"]},{"id":"d0a30f32.cf1b3","type":"debug","z":"b51d87d0.c86408","name":"no","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":810,"y":1460,"wires":[]},{"id":"a17fdd37.62873","type":"inject","z":"b51d87d0.c86408","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":1480,"wires":[["5afb409a.c968b"]]},{"id":"5afb409a.c968b","type":"function","z":"b51d87d0.c86408","name":"undefined","func":"node.send({payload: undefined});","outputs":1,"noerr":0,"initialize":"","finalize":"","x":400,"y":1480,"wires":[["a03fb66f.8b2a68"]]}]

```

affects work too

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [6 September 2020 09:35 UTC](https://discourse.nodered.org/t/null-instead-of-undefined/32490/12 "2020-09-06T09:35:30Z")

</div>

I seem to recall this is due to the fact we need to serialise the data to send it over the web socket to the browser, and it is not possible to represent undefined in any meaningful way, so it ends up being a null. I don’t think there is a way to fix this until there is a way to create a serialisable representation of undefined.

---

<div class="post-metadata">

**Author:** ![Daneev](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/daneev/32/10874_2.png) [@Daneev](https://discourse.nodered.org/u/Daneev)\
**Post date:** [6 September 2020 09:40 UTC](https://discourse.nodered.org/t/null-instead-of-undefined/32490/13 "2020-09-06T09:40:44Z")

</div>

I gave an example just above. there you can see that the error at the output of the function can affect the further logic of the process.

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [6 September 2020 09:46 UTC](https://discourse.nodered.org/t/null-instead-of-undefined/32490/14 "2020-09-06T09:46:29Z")

</div>

Out of curiosity I verified the issue and that it only affects the debug bar:

 ![2020_09_06_12.39.10](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/2/b2958890b7fe41ba93244f05c89c1f2b068cc92d.jpeg)

Both function nodes `console.log(msg.payload)` and debug node is set to output both to debug tab and its status.

@dceejay Status seems unaffected although it's part of the editor. But I guess that's just a `JSON.stringify` output and the debug tab shows actual (first serialized) objects?

(Sorry about the long screenshot, couldn't easily get a better one on my tablet)

---

<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:** [6 September 2020 09:47 UTC](https://discourse.nodered.org/t/null-instead-of-undefined/32490/15 "2020-09-06T09:47:21Z")

</div>

> [@Daneev](#):
>
> I gave an example just above. there you can see that the error at the output of the function can affect the further logic of the process.

Your switch node is looking for msg.payload undefined. msg.payload is not undefined it is an array containing one element which is itself undefined.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [6 September 2020 09:48 UTC](https://discourse.nodered.org/t/null-instead-of-undefined/32490/16 "2020-09-06T09:48:59Z")

</div>

Yes the status is already a string by the time it is sent.

---

<div class="post-metadata">

**Author:** ![Daneev](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/daneev/32/10874_2.png) [@Daneev](https://discourse.nodered.org/u/Daneev)\
**Post date:** [6 September 2020 09:51 UTC](https://discourse.nodered.org/t/null-instead-of-undefined/32490/17 "2020-09-06T09:51:30Z")

</div>

Yes, I'm sorry, I overlooked.  
this is how it works  
msg.payload = set [0];

---

<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:** [6 September 2020 09:53 UTC](https://discourse.nodered.org/t/null-instead-of-undefined/32490/18 "2020-09-06T09:53:59Z")

</div>

> [@Daneev](#):
>
> this is how it works  
> msg.payload = set [0];

This is how what works? Is your problem now solved?

---

<div class="post-metadata">

**Author:** ![Daneev](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/daneev/32/10874_2.png) [@Daneev](https://discourse.nodered.org/u/Daneev)\
**Post date:** [6 September 2020 09:54 UTC](https://discourse.nodered.org/t/null-instead-of-undefined/32490/19 "2020-09-06T09:54:53Z")

</div>

I agree, already convinced of this. but it doesn't look pretty.

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [6 September 2020 10:05 UTC](https://discourse.nodered.org/t/null-instead-of-undefined/32490/20 "2020-09-06T10:05:58Z")

</div>

I noticed a single property containing `undefined` (e.g. `msg.payload[0]` in this case prints fine. The serialisation works with them or there's some special handling?

I also tried sending

```auto
msg.payload = {foo: undefined, bar: null};

```

In this case the debug output was not

```auto
{
  foo: null,
  bar: null
}

```

as I expected but instead

```auto
{
  bar: null
}

```

Interesting issue indeed. Good to know about it.

[Next page](https://discourse.nodered.org/t/null-instead-of-undefined/32490.md?page=2)
