# Is global.set(var,value) required?

**URL:** <https://discourse.nodered.org/t/is-global-set-var-value-required/29690>\
**Category:** General\
**Created:** [8 July 2020 02:33 UTC](https://discourse.nodered.org/t/is-global-set-var-value-required/29690 "2020-07-08T02:33:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmisztur](https://avatars.discourse-cdn.com/v4/letter/c/53a042/32.png) [@cmisztur](https://discourse.nodered.org/u/cmisztur)\
**Post date:** [8 July 2020 02:33 UTC](https://discourse.nodered.org/t/is-global-set-var-value-required/29690/1 "2020-07-08T02:33:31Z")

</div>

In a function node I retrieve a global variable and assign it to a message property.

```auto
var a = global.get('var_a');
msg.a = a;

```

In another node I manipulate msg.a.  
I notice that next time I call global.get('var\_a'), the changes I made throughout the message's lifetime have persisted without calling global.set('var\_a',msg.a);

Is this expected?

---

<div class="post-metadata">

**Author:** ![SteveMann](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stevemann/32/3810_2.png) [@SteveMann](https://discourse.nodered.org/u/SteveMann)\
**Post date:** [8 July 2020 04:00 UTC](https://discourse.nodered.org/t/is-global-set-var-value-required/29690/2 "2020-07-08T04:00:39Z")

</div>

> [@cmisztur](#):
>
> ```auto
> var a = global.get('var_a');
> msg.a = a;
> 
> ```
> 
> In another node I manipulate msg.a.  
> I notice that next time I call global.get('var\_a'), the changes I made throughout the message's lifetime have persisted without calling global.set('var\_a',msg.a);

No. Unless I am reading your question wrong.  
Does this look like what you are asking:

```auto
[{"id":"e902913.398717","type":"inject","z":"1534182a.9ba0d8","name":"SET","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":240,"wires":[["5ad7a283.8c652c"]]},{"id":"5ad7a283.8c652c","type":"function","z":"1534182a.9ba0d8","name":"","func":"msg.a = 35;\nglobal.set('var_a',msg.a);\nreturn msg;","outputs":1,"noerr":0,"x":410,"y":240,"wires":[["f23b694d.075668"]],"info":"var a = global.get('var_a');\r\nmsg.a = a;\r\n\r\nIn another node I manipulate msg.a.\r\n\r\nI notice that next time I call global.get('var_a'), the changes I made throughout the message's lifetime have persisted without calling global.set('var_a',msg.a);"},{"id":"f23b694d.075668","type":"debug","z":"1534182a.9ba0d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":570,"y":240,"wires":[]},{"id":"c1761838.7e9e18","type":"inject","z":"1534182a.9ba0d8","name":"GET","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":360,"wires":[["7c242a54.e26d74"]]},{"id":"7c242a54.e26d74","type":"function","z":"1534182a.9ba0d8","name":"","func":"var var_a = global.get('var_a') || 0;\nmsg.a = var_a;\nreturn msg;","outputs":1,"noerr":0,"x":420,"y":360,"wires":[["13fcbf20.e21011"]]},{"id":"13fcbf20.e21011","type":"debug","z":"1534182a.9ba0d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":590,"y":360,"wires":[]},{"id":"b0dfc02.efe3d4","type":"inject","z":"1534182a.9ba0d8","name":"CHANGE msg.a","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":300,"wires":[["79d4a873.355fe8"]]},{"id":"79d4a873.355fe8","type":"function","z":"1534182a.9ba0d8","name":"","func":"msg.a=28;\nreturn msg;","outputs":1,"noerr":0,"x":400,"y":300,"wires":[["8a3a05f.0b0b4f8"]]},{"id":"8a3a05f.0b0b4f8","type":"debug","z":"1534182a.9ba0d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":580,"y":300,"wires":[]}]

```

I have three flows. One sets the global, one changes the var, and the last reads the global. It isn't changed.

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [8 July 2020 04:30 UTC](https://discourse.nodered.org/t/is-global-set-var-value-required/29690/3 "2020-07-08T04:30:56Z")

</div>

> [@cmisztur](#):
>
> Is this expected?

Yes, it is. Explanation can be found here:

> [@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:** ![cmisztur](https://avatars.discourse-cdn.com/v4/letter/c/53a042/32.png) [@cmisztur](https://discourse.nodered.org/u/cmisztur)\
**Post date:** [8 July 2020 21:37 UTC](https://discourse.nodered.org/t/is-global-set-var-value-required/29690/4 "2020-07-08T21:37:10Z")

</div>

That's convenient.

---

<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:** [22 July 2020 21:37 UTC](https://discourse.nodered.org/t/is-global-set-var-value-required/29690/5 "2020-07-22T21:37:11Z")

</div>

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