# Global.put not needed as variable altered by reference?

**URL:** https://discourse.nodered.org/t/global-put-not-needed-as-variable-altered-by-reference/20511
**Category:** General
**Created:** [17 January 2020 17:32 UTC](https://discourse.nodered.org/t/global-put-not-needed-as-variable-altered-by-reference/20511 "2020-01-17T17:32:24Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![coppo23](https://avatars.discourse-cdn.com/v4/letter/c/a4c791/32.png) [@coppo23](https://discourse.nodered.org/u/coppo23)
#### Post date: [17 January 2020 17:32 UTC](https://discourse.nodered.org/t/global-put-not-needed-as-variable-altered-by-reference/20511/1 "2020-01-17T17:32:24Z")

</div>

Hi  
From a general engineering background and writing a lot of functions in node red and learning as I go.  
I am very familar with C etc but finding JS a bit obscure at times and would appreciate some help.

I understand about javscript variables being by reference so a straight 'copy' only gets you another reference.

However I have a lot of global variable objects in my home automation system and in a lot of cases - I'll get a copy of status and fiddle with it - eg  
var thisDeviceStatus = global.get("maindevicestatus") ;  
thisDeviceStatus.someParameter = someValue;  
Then i'd put it back:- global.put(("maindevicestatus", thisDeviceStatus);

What I then discover is the global maindevicestatus.someParameter has also changed EVEN IF i excluded the global.put statement.

It seems this global.put is not necessary in this case. **Is this a correct statement?**

Many thanks

Coppo

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [17 January 2020 17:40 UTC](https://discourse.nodered.org/t/global-put-not-needed-as-variable-altered-by-reference/20511/2 "2020-01-17T17:40:32Z")

</div>

> [@coppo23](#):
>
> finding JS a bit obscure at times

Well, that is probably the understatement of the new decade! 😀

> [@coppo23](#):
>
> It seems this global.put is not necessary in this case. **Is this a correct statement?**

Well ... yes, sort-of.

While perhaps not strictly required right now, it may well become so in the future (near or far!). Node-RED may, in the future, act to better isolate context variables as newer versions of Node.js (JavaScript) become the standard.

Also, it _is_ necessary if you have more than one context storage method defined. Since you have to tell it which to write to (otherwise, it writes to the default).

So it is good practice in all cases and necessary in some.

---

<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: [17 January 2020 22:05 UTC](https://discourse.nodered.org/t/global-put-not-needed-as-variable-altered-by-reference/20511/3 "2020-01-17T22:05:59Z")

</div>

It is also necessary to use `global.set` if the variable is a simple value, such as a number or a string, rather than a javascript object.

---

<div class="post-metadata">

### Author: ![coppo23](https://avatars.discourse-cdn.com/v4/letter/c/a4c791/32.png) [@coppo23](https://discourse.nodered.org/u/coppo23)
#### Post date: [17 January 2020 22:27 UTC](https://discourse.nodered.org/t/global-put-not-needed-as-variable-altered-by-reference/20511/4 "2020-01-17T22:27:41Z")

</div>

Thanks. Do you know exactly what conditions cause the global variable to be updated when I edit the local 'copy'?

This is what alerted me to the problem - setting A 'DIRTY' flag in my local 'copy' of the global and dicsovering the global object DIRTY flag was also set! I didn't want the global version changed.

Most of my global data is objects.  
Cheers  
Coppo

---

<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: [17 January 2020 22:52 UTC](https://discourse.nodered.org/t/global-put-not-needed-as-variable-altered-by-reference/20511/5 "2020-01-17T22:52:17Z")

</div>

> [@coppo23](#):
>
> Do you know exactly what conditions cause the global variable to be updated when I edit the local 'copy'?

JavaScript passes objects around by reference rather than by value. So the call to `global.get()` returns a reference to the object in context. Any modification of the object will modify the object in context.

If you want to modify the object without modifying what is in context, you have to clone it first. We provide the `RED.util.cloneMessage` function that can be used for this:

```auto
var value = RED.util.cloneMessage( global.get("myValue") );

```

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [18 January 2020 10:27 UTC](https://discourse.nodered.org/t/global-put-not-needed-as-variable-altered-by-reference/20511/6 "2020-01-18T10:27:55Z")

</div>

However, I assume that isn't quite the case if you are using the file-based context store. Rather the in-memory copy of the variable is treated the same but not the file version.

---

<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: [18 January 2020 10:40 UTC](https://discourse.nodered.org/t/global-put-not-needed-as-variable-altered-by-reference/20511/7 "2020-01-18T10:40:15Z")

</div>

You assume wrong. The file store uses an in memory cache in front of it.

---

<div class="post-metadata">

### Author: ![coppo23](https://avatars.discourse-cdn.com/v4/letter/c/a4c791/32.png) [@coppo23](https://discourse.nodered.org/u/coppo23)
#### Post date: [18 January 2020 10:44 UTC](https://discourse.nodered.org/t/global-put-not-needed-as-variable-altered-by-reference/20511/8 "2020-01-18T10:44:33Z")

</div>

Thanks folks. I do understand the referencing but not completely clear about when it is and isn't a 'by reference' variable. What I was trying to find out what is safe because I was seeing inconsistancy!

My understanding from what you have all said is that I always need to follow the following rules:-

a) If I want to update a global variable I **always** use global.put -( even though most of the time it wouldn't be needed)  
b) if i don't want to update the global I **clone it first**

Does that cover it?  
Cheers  
Coppo

---

<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: [18 January 2020 10:47 UTC](https://discourse.nodered.org/t/global-put-not-needed-as-variable-altered-by-reference/20511/9 "2020-01-18T10:47:10Z")

</div>

This only applies to Array and Object types.

If your context value is a string, boolean or number, and is at the top level of context (ie not a property inside an object or array), then you don't need to clone.
