# Help with reading any context value and the "check not null" option

**URL:** <https://discourse.nodered.org/t/help-with-reading-any-context-value-and-the-check-not-null-option/33484>\
**Category:** General\
**Created:** [29 September 2020 00:07 UTC](https://discourse.nodered.org/t/help-with-reading-any-context-value-and-the-check-not-null-option/33484 "2020-09-29T00:07:08Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [29 September 2020 00:07 UTC](https://discourse.nodered.org/t/help-with-reading-any-context-value-and-the-check-not-null-option/33484/1 "2020-09-29T00:07:08Z")

</div>

I just wasted about 45 minutes on a stupid mistake - again.

So, I am reading a _context_ value, and need it to be something rather than `undefined`.

If it is a number, the option is something like:

```auto
let x = context.get("blah") || 0;

```

Which means if `blah` isn't set (for what ever reason) `x` is given the value of `0`.

I'm not exactly sure about text, but for now, I will let that go (though I know it will bite me soon).

Say I am doing something and it has a valid range of 0 - 255.  
So `0` is as valid as `1`, or `255`.

But I want the value returned if the _context_ isn't set to be .... say 123. (Half 255)

How do I do that?

I can't set it to `0` then say `if == 0 make it 123` because if it **is** `0` it will be changed to `123`.

I am missing something it would seem.

---

<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:** [29 September 2020 01:34 UTC](https://discourse.nodered.org/t/help-with-reading-any-context-value-and-the-check-not-null-option/33484/2 "2020-09-29T01:34:07Z")

</div>

Did you try  
let x = context.get("blah") || 123;

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [29 September 2020 01:36 UTC](https://discourse.nodered.org/t/help-with-reading-any-context-value-and-the-check-not-null-option/33484/3 "2020-09-29T01:36:54Z")

</div>

Yes, and it drove me around the twist for 45 minutes because when `blah` was equal to `0` it got changed to 123. Or something like that.

Then I realised what was going on and make the last part ` || 0` and the problem went away.

---

<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:** [29 September 2020 05:47 UTC](https://discourse.nodered.org/t/help-with-reading-any-context-value-and-the-check-not-null-option/33484/4 "2020-09-29T05:47:11Z")

</div>

That’s right, the retrieved value is evaluated. Zero (0) evaluates to false so it would then use the 123. It’s JavaScript coercion:

- `A || B` returns the value _A_ if _A_ can be coerced into `true` ; otherwise, it returns _B_ .

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [29 September 2020 06:05 UTC](https://discourse.nodered.org/t/help-with-reading-any-context-value-and-the-check-not-null-option/33484/5 "2020-09-29T06:05:07Z")

</div>

Ok, thanks.

It would seem I didn't understand the problem and so asked the wrong question - way back then.

The _solution_ I was given worked for that case and as a result I (wrongly) used it for other cases.

Without wanting to start a new thread, can someone help me with this new question:

I want a line (or so) of code (I hope one) that can be used when testing the existence of _context_ variables. (context, flow or global)

I want to test for the existence and if the variable doesn't exist returns something.  
This could be text, number or logic (`true`/`false`)

Then I could maybe use it in future in stead of that which I am using and avoiding the mistake/s I made today from happening again.

---

<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:** [29 September 2020 08:44 UTC](https://discourse.nodered.org/t/help-with-reading-any-context-value-and-the-check-not-null-option/33484/6 "2020-09-29T08:44:01Z")

</div>

The `|| value` technique only works if `value` is zero or if zero is not a valid value for your system. If you need to initialise it to 123 and 0 is a valid value then you need to `get` it and then test for `undefined` using an explicit `if` test such as `if (x == undefined) {`

---

<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:** [29 September 2020 08:46 UTC](https://discourse.nodered.org/t/help-with-reading-any-context-value-and-the-check-not-null-option/33484/7 "2020-09-29T08:46:47Z")

</div>

Hi @Trying_to_learn

If a value doesn't exist, then the call to `context.get("blah")` will return `undefined`.

You will need to explicitly test for that:

```auto
let x = context.get("blah");
if (x === undefined) { x = 123; }

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [30 September 2020 06:22 UTC](https://discourse.nodered.org/t/help-with-reading-any-context-value-and-the-check-not-null-option/33484/8 "2020-09-30T06:22:56Z")

</div>

Thanks @knolleary and @Colin.

I shall _roll that out_ as my way of testing from now on.

---

<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:** [14 October 2020 06:23 UTC](https://discourse.nodered.org/t/help-with-reading-any-context-value-and-the-check-not-null-option/33484/9 "2020-10-14T06:23:23Z")

</div>

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