# How can check the null or undefined values

**URL:** <https://discourse.nodered.org/t/how-can-check-the-null-or-undefined-values/25598>\
**Category:** General\
**Created:** [27 April 2020 07:44 UTC](https://discourse.nodered.org/t/how-can-check-the-null-or-undefined-values/25598 "2020-04-27T07:44:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahd](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Ahd](https://discourse.nodered.org/u/Ahd)\
**Post date:** [27 April 2020 07:44 UTC](https://discourse.nodered.org/t/how-can-check-the-null-or-undefined-values/25598/1 "2020-04-27T07:44:48Z")

</div>

How to determine if one variable of the payload is undefined or null in JavaScript then must not send it to DB.

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [27 April 2020 07:53 UTC](https://discourse.nodered.org/t/how-can-check-the-null-or-undefined-values/25598/2 "2020-04-27T07:53:59Z")

</div>

In a `function` node, you could do something like:

```auto
if (typeof msg.payload.myVal != 'undefined' && msg.payload.myVal !== null) {
   return msg;
}

```

Or alternatively, you can use a `switch` node with the 'not null' condition.

---

<div class="post-metadata">

**Author:** ![Ahd](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Ahd](https://discourse.nodered.org/u/Ahd)\
**Post date:** [27 April 2020 08:18 UTC](https://discourse.nodered.org/t/how-can-check-the-null-or-undefined-values/25598/3 "2020-04-27T08:18:03Z")

</div>

Thank @kuema

---

<div class="post-metadata">

**Author:** ![hklages](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hklages/32/23899_2.png) [@hklages](https://discourse.nodered.org/u/hklages)\
**Post date:** [29 April 2020 20:26 UTC](https://discourse.nodered.org/t/how-can-check-the-null-or-undefined-values/25598/4 "2020-04-29T20:26:57Z")

</div>

Very often also NaN, +/-infinite cause problems. Therefore I use this small function:

```auto
isTruthy: input => {
    return !(typeof input === 'undefined' || input === null ||
      (typeof input === 'number' && !Number.isFinite(input)))
  },

```

... and for properties (example msg.payload.result --\> isValidProperty(msg, ['paylaod', 'result']

```auto
isValidProperty: (nestedObj, pathArray) => {
    const property = pathArray.reduce(
      (obj, key) => (obj && obj[key] !== 'undefined' ? obj[key] : undefined),
      nestedObj
    )
    return isTruthy(property)

```

---

<div class="post-metadata">

**Author:** ![carlosridg](https://avatars.discourse-cdn.com/v4/letter/c/839c29/32.png) [@carlosridg](https://discourse.nodered.org/u/carlosridg)\
**Post date:** [9 March 2021 07:29 UTC](https://discourse.nodered.org/t/how-can-check-the-null-or-undefined-values/25598/5 "2021-03-09T07:29:36Z")

</div>

The value 'undefined' denotes that a variable has been declared, but hasn't been assigned any value. On the other hand, [Javascript null](http://net-informations.com/js/iq/nuvsun.htm) refers to a non-existent object, which basically means 'empty' or 'nothing'. They're both values usually used to indicate the absence of something . Also, undefined and null are two distinct types: undefined is a type itself (undefined) while null is an object .

---

<div class="post-metadata">

**Author:** ![softy2k](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/softy2k/32/23707_2.png) [@softy2k](https://discourse.nodered.org/u/softy2k)\
**Post date:** [9 March 2021 09:56 UTC](https://discourse.nodered.org/t/how-can-check-the-null-or-undefined-values/25598/6 "2021-03-09T09:56:59Z")

</div>

![T9M2J](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/7/707a9752ad18107cf0ff7f607ec568d212aebf9c.png)

---

<div class="post-metadata">

**Author:** ![JayDL](https://avatars.discourse-cdn.com/v4/letter/j/f08c70/32.png) [@JayDL](https://discourse.nodered.org/u/JayDL)\
**Post date:** [7 December 2022 23:15 UTC](https://discourse.nodered.org/t/how-can-check-the-null-or-undefined-values/25598/7 "2022-12-07T23:15:50Z")

</div>

> [@kuema](#):
>
> `if (typeof msg.payload.myVal != 'undefined' && msg.payload.myVal !== null) {`

This works great for me,

```auto
if (typeof msg.payload.myVal != 'undefined') {
    msg.payload.myVal = 1;
}
return msg;

```

But, this does not,

```auto
if (typeof msg.payload.myVal != 'undefined') {
    msg.payload.myVal = 1;
} else {
    msg.payload.myVal = 0;
}
return msg;

```

The else never gets called when the condition isn't false.

If I change the `!=` to `==` and reverse the if and the else blocks it still doesn't work, the else block simply doesn't get called.

My solution was the following:

```auto
msg.payload.myVal = 0;
if (typeof msg.payload != 'undefined') {
    msg.payload.myVal = 1;
}
return msg;

```

I have no idea why, but this information might help someone in the future.

Cheers!

---

<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:** [8 December 2022 00:20 UTC](https://discourse.nodered.org/t/how-can-check-the-null-or-undefined-values/25598/8 "2022-12-08T00:20:31Z")

</div>


