# Pushing a local var updates the global one

**URL:** <https://discourse.nodered.org/t/pushing-a-local-var-updates-the-global-one/21414>\
**Category:** General\
**Created:** [8 February 2020 05:50 UTC](https://discourse.nodered.org/t/pushing-a-local-var-updates-the-global-one/21414 "2020-02-08T05:50:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lupin\_III](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lupin_iii/32/67650_2.png) [@Lupin\_III](https://discourse.nodered.org/u/Lupin_III)\
**Post date:** [8 February 2020 05:50 UTC](https://discourse.nodered.org/t/pushing-a-local-var-updates-the-global-one/21414/1 "2020-02-08T05:50:58Z")

</div>

Hi, Im spending lots of hours without success nor understanding why it happens.  
I have a global variable (an array) stored in filesystem. In a function node I put a local variable equal to the global one. Then, working on the local variable I push the incoming message, but at each deployng the the global variable is updated. I don't put global.set(...) and I don't want to change the global variable. How can I solve it? Thanks a lot  
This are the function node and the global variables before deploying

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/c/6c40f49fa5c41a1913c77f15dfffc36db3d148c9.png)  
This are the debug nodes and the global variables after the first deploying  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/3/83b27e7846ec4483eeec615481aff7aa3d8c4643.png)  
This are the debug nodes and the global variables after the second deploying  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/9/4951106dbaa3a77f6a555fadb0837fb972bada28.png)

---

<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:** [8 February 2020 08:17 UTC](https://discourse.nodered.org/t/pushing-a-local-var-updates-the-global-one/21414/2 "2020-02-08T08:17:10Z")

</div>

Please see this topic for the explanation of this behaviour:

> [@Global.put not needed as variable altered by reference?](https://discourse.nodered.org/t/global-put-not-needed-as-variable-altered-by-reference/20511/5):
>
> 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: var value = RED.util.cloneMessage( global.get("myValue") );

---

<div class="post-metadata">

**Author:** ![Lupin\_III](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lupin_iii/32/67650_2.png) [@Lupin\_III](https://discourse.nodered.org/u/Lupin_III)\
**Post date:** [8 February 2020 10:44 UTC](https://discourse.nodered.org/t/pushing-a-local-var-updates-the-global-one/21414/3 "2020-02-08T10:44:39Z")

</div>

Thanks Knolleary. This happen only for global, not for flow, is that correct?  
Thanks a lot

---

<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:** [8 April 2020 10:44 UTC](https://discourse.nodered.org/t/pushing-a-local-var-updates-the-global-one/21414/4 "2020-04-08T10:44:52Z")

</div>

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