# Global.get('')||0 inconsistent resutls (Function node)

**URL:** https://discourse.nodered.org/t/global-get-0-inconsistent-resutls-function-node/68457
**Category:** General
**Created:** [1 October 2022 15:21 UTC](https://discourse.nodered.org/t/global-get-0-inconsistent-resutls-function-node/68457 "2022-10-01T15:21:24Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![edterbak](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edterbak/32/60017_2.png) [@edterbak](https://discourse.nodered.org/u/edterbak)
#### Post date: [1 October 2022 15:21 UTC](https://discourse.nodered.org/t/global-get-0-inconsistent-resutls-function-node/68457/1 "2022-10-01T15:21:24Z")

</div>

Hello everyone,

Im using Function node a lot.

But I am seeing irratic behaviour of node red. Atleast thats how I percieve it. Might be due to a lack of understading 🙂

```auto
var nr_corr = global.get('NightReduction_current_value',"file");
msg.payload = nr_corr;
return msg;

```

Debug node after and inject node in front shows this produces value 0. As expected. It is also the stored value.

If I want to have a default value of 10 when the variable is undefined, I was thinking that adding "||10 " would be enough. like below

```auto
var nr_corr = global.get('NightReduction_current_value',"file")||10;
msg.payload = nr_corr;
return msg;

```

I expect it to produce 0 now. Because the value is known as shown in the first example.  
But the debug node shows value 10 instead.

Is there something I am missing?  
This behaviour is sometimes there, sometimes not.

Im currently using NodeRed version 3.0.2

many thanks in advance 😄

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [1 October 2022 15:27 UTC](https://discourse.nodered.org/t/global-get-0-inconsistent-resutls-function-node/68457/2 "2022-10-01T15:27:55Z")

</div>

Maybe you need to use persistent context?

[https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115)

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [1 October 2022 15:29 UTC](https://discourse.nodered.org/t/global-get-0-inconsistent-resutls-function-node/68457/3 "2022-10-01T15:29:29Z")

</div>

The number 0 is considered false, so if you you fetch `0` the or would kick in, as it is considered false.. Therefore it defaults to 10.  
You will need to use if else conditional to set a default. If `undefined`

---

<div class="post-metadata">

### Author: ![edterbak](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edterbak/32/60017_2.png) [@edterbak](https://discourse.nodered.org/u/edterbak)
#### Post date: [1 October 2022 15:33 UTC](https://discourse.nodered.org/t/global-get-0-inconsistent-resutls-function-node/68457/4 "2022-10-01T15:33:00Z")

</div>

So you are saying that every variable I have declared in global.set ("file") that might be set to the value 0 at some time, will always be showing the ||10 \>\> value 10 as result ??

whuuuuht? 😃

---

<div class="post-metadata">

### Author: ![edterbak](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edterbak/32/60017_2.png) [@edterbak](https://discourse.nodered.org/u/edterbak)
#### Post date: [1 October 2022 15:33 UTC](https://discourse.nodered.org/t/global-get-0-inconsistent-resutls-function-node/68457/5 "2022-10-01T15:33:48Z")

</div>

I am using persistent storage. But I use global variables. I work with multiple tabs. big flows. Thats why I use global instad of flow or context.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [1 October 2022 15:35 UTC](https://discourse.nodered.org/t/global-get-0-inconsistent-resutls-function-node/68457/6 "2022-10-01T15:35:16Z")

</div>

> [@edterbak](#):
>
> So you are saying that every variable I have declared in global.set ("file") that might be set to the value 0 at some time, will always be showing the ||10 \>\> value 10 as result ??

Yes, That's what i am saying. ( would have stopped at Yes but min 10 characters)

---

<div class="post-metadata">

### Author: ![edterbak](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edterbak/32/60017_2.png) [@edterbak](https://discourse.nodered.org/u/edterbak)
#### Post date: [1 October 2022 15:36 UTC](https://discourse.nodered.org/t/global-get-0-inconsistent-resutls-function-node/68457/7 "2022-10-01T15:36:16Z")

</div>

You will need to use if else conditional to set a default. If `undefined`

OK. 😵‍💫  
I have just done this ||0 at multiple places, becasue I thought it was a better and shorter way of testing if the variable exists. hahahaha..

---

<div class="post-metadata">

### Author: ![edterbak](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edterbak/32/60017_2.png) [@edterbak](https://discourse.nodered.org/u/edterbak)
#### Post date: [1 October 2022 15:37 UTC](https://discourse.nodered.org/t/global-get-0-inconsistent-resutls-function-node/68457/8 "2022-10-01T15:37:54Z")

</div>

Thank you all for the quick reply!! Much appreciated. 🙂

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [1 October 2022 15:39 UTC](https://discourse.nodered.org/t/global-get-0-inconsistent-resutls-function-node/68457/9 "2022-10-01T15:39:02Z")

</div>

` || 0` is fine but not any other number, unless there will never be a `0`.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [1 October 2022 15:40 UTC](https://discourse.nodered.org/t/global-get-0-inconsistent-resutls-function-node/68457/10 "2022-10-01T15:40:40Z")

</div>

> [@E1cid](#):
>
> The number 0 is considered false, so if you you fetch `0` the or would kick in, as it is considered false.. Therefore it defaults to 10.  
> You will need to use if else conditional to set a default. If `undefined`

Hmm. Is that one of those non-positive undocumented features?  
It's a bit of a landmine when a variable might validly acquire a value of 0.

So we should never use **context.get('variable') || default\_number**?

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [1 October 2022 15:42 UTC](https://discourse.nodered.org/t/global-get-0-inconsistent-resutls-function-node/68457/11 "2022-10-01T15:42:11Z")

</div>

> [@jbudd](#):
>
> So we should never use **context.get('variable') || default\_number**?

see above [post](https://discourse.nodered.org/t/global-get-0-inconsistent-resutls-function-node/68457/9)

---

<div class="post-metadata">

### Author: ![edterbak](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edterbak/32/60017_2.png) [@edterbak](https://discourse.nodered.org/u/edterbak)
#### Post date: [1 October 2022 15:47 UTC](https://discourse.nodered.org/t/global-get-0-inconsistent-resutls-function-node/68457/12 "2022-10-01T15:47:18Z")

</div>

```auto
var ___test = global.get('___testvalue0')||10;
msg.payload = ___test;
return msg;

```

Result = 10

```auto
var ___test = global.get('___testvalue1')||10;
msg.payload = ___test;
return msg;

```

Result = 1

E1cid is 100% correct. Thank you for this clarity.

@E1cid So any stored value in context/flow/global is subjected to this logic right?

|| is never a solution to check if the varible exists/undefined.

---

<div class="post-metadata">

### Author: ![edterbak](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edterbak/32/60017_2.png) [@edterbak](https://discourse.nodered.org/u/edterbak)
#### Post date: [1 October 2022 15:50 UTC](https://discourse.nodered.org/t/global-get-0-inconsistent-resutls-function-node/68457/13 "2022-10-01T15:50:47Z")

</div>

Just as a sneaky note:

The only case you can use || to do this, is when you have ||0  
Hahaha..  
If the value is 0, it will be set to 0  
if the value is undefined, it will be set to 0 as well 😃

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [1 October 2022 15:56 UTC](https://discourse.nodered.org/t/global-get-0-inconsistent-resutls-function-node/68457/14 "2022-10-01T15:56:19Z")

</div>

> [@edterbak](#):
>
> So any stored value in context/flow/global is subjected to this logic right?

Any variable, not just context. This is the Javascript logic

> **[Logical OR (||) - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR#description)**
>
> The logical OR (||) (logical disjunction) operator for a set of operands
> is true if and only if one or more of its operands is true. It is typically used with
> boolean (logical) values. When it is, it returns a Boolean value. However,
> the ||...

> **[Falsy - MDN Web Docs Glossary: Definitions of Web-related terms | MDN](https://developer.mozilla.org/en-US/docs/Glossary/Falsy)**
>
> A falsy (sometimes written falsey) value is a value that is considered false when encountered in a Boolean context.

> [@edterbak](#):
>
> Just as a sneaky note:
> 
> The only case you can use || to do this, is when you have ||0  
> Hahaha..  
> If the value is 0, it will be set to 0  
> if the value is undefined, it will be set to 0 as well 😃

@edterbak see post [above](https://discourse.nodered.org/t/global-get-0-inconsistent-resutls-function-node/68457/9)

---

<div class="post-metadata">

### Author: ![edterbak](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edterbak/32/60017_2.png) [@edterbak](https://discourse.nodered.org/u/edterbak)
#### Post date: [1 October 2022 16:02 UTC](https://discourse.nodered.org/t/global-get-0-inconsistent-resutls-function-node/68457/15 "2022-10-01T16:02:13Z")

</div>

After reading the links you gave, I **think** I am right. that I CAN only use the "||0".

Can you confirm that?

In general, I store global varibles. numbers a lot. So in that case 0 would still produce 0 right?  
(Difficult matter this is 🙂 )

And again E1cid! thank you very much in sharing your knowledge

---

<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: [15 October 2022 16:02 UTC](https://discourse.nodered.org/t/global-get-0-inconsistent-resutls-function-node/68457/16 "2022-10-15T16:02:21Z")

</div>

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