# || operator causes problems with context.get()

**URL:** <https://discourse.nodered.org/t/operator-causes-problems-with-context-get/14395>\
**Category:** General\
**Created:** [16 August 2019 14:02 UTC](https://discourse.nodered.org/t/operator-causes-problems-with-context-get/14395 "2019-08-16T14:02:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gnamra](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gnamra](https://discourse.nodered.org/u/Gnamra)\
**Post date:** [16 August 2019 14:02 UTC](https://discourse.nodered.org/t/operator-causes-problems-with-context-get/14395/1 "2019-08-16T14:02:42Z")

</div>

I've got a function node that has two inputs one output and it looks like this.

```auto
var allowThroughput = context.get("allowThroughput");

node.log("allowThroughput: " + allowThroughput)

if(msg.payload == "changeThroughput")
{
  allowThroughput = !allowThroughput;
  node.log("Changed allowThroughput: " + allowThroughput)
}

context.set("allowThroughput", allowThroughput)

if(allowThroughput && msg.payload != "changeThroughput" )
    return msg;
else
    return null;

```

The purpose of this function is to stop the throughput of messages if I press the inject node connected to it. Because for some bizarre reason I can't stop or pause my flow, which would've been a very handy thing to have while prototyping. So I've made this as a workaround to stop looping behaviour.

The second input is a function that sends 1 or 0 to light up an LED that I've got connected to my raspberry pi. The problem is that when I change it to initialize allowThroughput if it doesn't exist like so:

```auto
var allowThroughput = context.get("allowThroughput")||true;

```

then it stops working.

Below I've copied the output from my function.

Without initializing:

```auto
16 Aug 15:58:13 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:58:13 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:58:14 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:58:14 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:58:14 - [info] [function:Loop Controller] Changed allowThroughput: false
16 Aug 15:58:14 - [info] [function:Loop Controller] allowThroughput: false
16 Aug 15:58:15 - [info] [function:Loop Controller] allowThroughput: false
16 Aug 15:58:15 - [info] [function:Loop Controller] Changed allowThroughput: true
16 Aug 15:58:16 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:58:16 - [info] [function:Loop Controller] Changed allowThroughput: false
16 Aug 15:58:16 - [info] [function:Loop Controller] allowThroughput: false
16 Aug 15:58:16 - [info] [function:Loop Controller] Changed allowThroughput: true

```

As you can see, it correctly stops when I press the inject button, and I am able to control the allowThroughput variable.

With initializing:

```auto
16 Aug 15:59:33 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:33 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:33 - [info] [function:Loop Controller] Changed allowThroughput: false
16 Aug 15:59:33 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:33 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:33 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:33 - [info] [function:Loop Controller] Changed allowThroughput: false
16 Aug 15:59:33 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:34 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:34 - [info] [function:Loop Controller] Changed allowThroughput: false
16 Aug 15:59:34 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:34 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:34 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:34 - [info] [function:Loop Controller] Changed allowThroughput: false
16 Aug 15:59:34 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:34 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:34 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:35 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:35 - [info] [function:Loop Controller] Changed allowThroughput: false
16 Aug 15:59:35 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:35 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:35 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:35 - [info] [function:Loop Controller] Changed allowThroughput: false
16 Aug 15:59:35 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:35 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:35 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:35 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:35 - [info] [function:Loop Controller] Changed allowThroughput: false
16 Aug 15:59:36 - [info] [function:Loop Controller] allowThroughput: true
16 Aug 15:59:36 - [info] [function:Loop Controller] allowThroughput: true

```

As you can see I am no longer able to control the allowThroughput variable. It is set to true no matter what. Can someone explain to me why this is?

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [16 August 2019 14:08 UTC](https://discourse.nodered.org/t/operator-causes-problems-with-context-get/14395/2 "2019-08-16T14:08:36Z")

</div>

`var allowThroughput = context.get("allowThroughput");`  
`allowThroughput = !allowThroughput;`  
What if it is empty ?

---

<div class="post-metadata">

**Author:** ![Gnamra](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gnamra](https://discourse.nodered.org/u/Gnamra)\
**Post date:** [16 August 2019 14:11 UTC](https://discourse.nodered.org/t/operator-causes-problems-with-context-get/14395/4 "2019-08-16T14:11:41Z")

</div>

Initializing it breaks it. That's why I am asking this question, I'd like to initialize it.

If I leave it undefined, then the program doesn't run properly until I manually initialize it by pressing it once or twice to set the value to true.

---

<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:** [16 August 2019 14:28 UTC](https://discourse.nodered.org/t/operator-causes-problems-with-context-get/14395/5 "2019-08-16T14:28:48Z")

</div>

> [@Gnamra](#):
>
> var allowThroughput = context.get("allowThroughput")||true;

That says pickup the value from the context and test it. If it is a value such as not defined or 0 or, in particular, false then it will set it to true. The result is that it always picks up true. You need to fetch it and test for undefined instead.

---

<div class="post-metadata">

**Author:** ![Gnamra](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gnamra](https://discourse.nodered.org/u/Gnamra)\
**Post date:** [16 August 2019 14:33 UTC](https://discourse.nodered.org/t/operator-causes-problems-with-context-get/14395/6 "2019-08-16T14:33:56Z")

</div>

That makes sense. I'm not familiar with JavaScript, so perhaps some rewording at [https://nodered.org/docs/user-guide/writing-functions#storing-data](https://nodered.org/docs/user-guide/writing-functions#storing-data) should be considered.

```auto
// initialise the counter to 0 if it doesn't exist already

```

Gave me the impression that it would only set it to 0 if it didn't exist. Though I should've figured since I am familiar with || from other languages. Thanks for your help!

---

<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:** [16 August 2019 15:33 UTC](https://discourse.nodered.org/t/operator-causes-problems-with-context-get/14395/7 "2019-08-16T15:33:14Z")

</div>

In the particular case shown the comment is correct, as the value is being set to 0. If you were initialising it to false it would be ok.
