|| operator causes problems with context.get()

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

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:

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

then it stops working.

Below I've copied the output from my function.

Without initializing:

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:

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?

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

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.

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.

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

// 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!

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.