# Question on global context & function behaviour

**URL:** https://discourse.nodered.org/t/question-on-global-context-function-behaviour/31383
**Category:** General
**Created:** [10 August 2020 10:32 UTC](https://discourse.nodered.org/t/question-on-global-context-function-behaviour/31383 "2020-08-10T10:32:23Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![duaw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/duaw/32/20970_2.png) [@duaw](https://discourse.nodered.org/u/duaw)
#### Post date: [10 August 2020 10:32 UTC](https://discourse.nodered.org/t/question-on-global-context-function-behaviour/31383/1 "2020-08-10T10:32:23Z")

</div>

Hi,  
I have a problem using context. Consider this flow:

```auto
[{"id":"c966d72c.b3b428","type":"function","z":"d4de0d13.1abea8","name":"get global dummy","func":"var dummy = global.get(\"dummy\")||10;\nmsg.payload = dummy;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":770,"y":300,"wires":[["375b567e.93a7d2"]]},{"id":"c069bc8e.88a7b8","type":"change","z":"d4de0d13.1abea8","name":"set global.dummy","rules":[{"t":"set","p":"dummy","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":300,"wires":[["c966d72c.b3b428"]]},{"id":"cdadfa02.84ae4","type":"inject","z":"d4de0d13.1abea8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":350,"y":280,"wires":[["c069bc8e.88a7b8"]]},{"id":"4b751bf8.bf39e4","type":"inject","z":"d4de0d13.1abea8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"2","payloadType":"num","x":350,"y":320,"wires":[["c069bc8e.88a7b8"]]},{"id":"375b567e.93a7d2","type":"debug","z":"d4de0d13.1abea8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":990,"y":300,"wires":[]},{"id":"235495da.cc389a","type":"inject","z":"d4de0d13.1abea8","name":"get global dummy","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":560,"y":180,"wires":[["c966d72c.b3b428"]]}]

```

I start with no global context "dummy".  
When I try to get it, the functions defaults the value to 10:  
var dummy = global.get("dummy")||10;

When I inject 2, global context is set to 2, the subsequent function yields 2. Ok.

But when I inject 0, the global value is set to 0, however, the function yields 10.

Why is this the case? I am not a JS expert and all I read so far was that "||10" is useful when the part to the left of it is undefined. Is that wrong?  
How can I fix that?

Thanks in advance, Uwe

---

<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: [10 August 2020 10:36 UTC](https://discourse.nodered.org/t/question-on-global-context-function-behaviour/31383/2 "2020-08-10T10:36:35Z")

</div>

> [@duaw](#):
>
> that "||10" is useful when the part to the left of it is undefined

Not quite. It is useful when the left is a false-like value, which includes undefined. But it also includes the number 0, the empty string "" , null and actual Boolean false.

If 0 is a valid value, but you want the default to be non 0, then you'll need to do a more explicit test:

```auto
var dummy = global.get("dummy");
if (dummy === undefined) {
   dummy = 10;
}

```

---

<div class="post-metadata">

### Author: ![duaw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/duaw/32/20970_2.png) [@duaw](https://discourse.nodered.org/u/duaw)
#### Post date: [10 August 2020 10:41 UTC](https://discourse.nodered.org/t/question-on-global-context-function-behaviour/31383/3 "2020-08-10T10:41:56Z")

</div>

Thanks for that super quick answer!

I bet this is something others trapped over, too, or will, particularly those discovering NR without proper JS background.  
Ciao, Uwe

---

<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: [9 October 2020 10:42 UTC](https://discourse.nodered.org/t/question-on-global-context-function-behaviour/31383/4 "2020-10-09T10:42:03Z")

</div>

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