# Why is the global variable changing?

**URL:** <https://discourse.nodered.org/t/why-is-the-global-variable-changing/90759>\
**Category:** General\
**Tags:** possible-bug\
**Created:** [6 September 2024 19:31 UTC](https://discourse.nodered.org/t/why-is-the-global-variable-changing/90759 "2024-09-06T19:31:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tkreks](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tkreks/32/40052_2.png) [@tkreks](https://discourse.nodered.org/u/tkreks)\
**Post date:** [6 September 2024 19:31 UTC](https://discourse.nodered.org/t/why-is-the-global-variable-changing/90759/1 "2024-09-06T19:31:55Z")

</div>

Hi!  
I have written a function that should compare json msg.a and msg.b . The differences are displayed in msg.c. And msg.b is updated with values from msg.a . But I take these values from global variables 1 and 2. After my function is executed, I get the correct result. But, the global variable 2 is updated without my participation, without global.set . how is this possible? Why is this happening? For example, I recorded a video - [Youtube](https://youtu.be/uuXmpkqV04Y?si=QiH_Brt2T-H0E22A)  
The code of my function

> msg.c = {};  
> for (let key in msg.a) {  
> if (msg.b.hasOwnProperty(key)) {  
> if (msg.a[key] !== msg.b[key]) {  
> msg.c[key] = msg.a[key];  
> msg.b[key] = msg.a[key];  
> }  
> } else {  
> msg.c[key] = msg.a[key];  
> msg.b[key] = msg.a[key];  
> }  
> }  
> return msg;

My flow in attach  
[flows.json](https://discourse.nodered.org/uploads/short-url/y1CUKFGaKh60fjIOuuyCZkfgGdN.json) (4.8 KB)

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [6 September 2024 19:48 UTC](https://discourse.nodered.org/t/why-is-the-global-variable-changing/90759/2 "2024-09-06T19:48:25Z")

</div>

Sorry, but I took one look at your flow, and was dizzy!

- Copy this to that
- Copy that to this
- Set this to that
- Set that to this

😵

But as a hint: objects are `pass-by-ref` - meaning, if its not a primitive type (`string`, `int` etc etc)

The object(s) you are referencing are only passing you the address (in memory) to that object.

so if anything is updating an `object` (and not a primitive type) its will update that object - no matter where it is in your code/flow

you might need to look at

```auto
RED.util.cloneMessage(<Object>);

```

This creates an un-attached copy of the original object, meaning manipulating it's result, wont alter the original one in your global reference

This is normal for javascript - and isn't a Node RED "possible-bug"

---

<div class="post-metadata">

**Author:** ![tkreks](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tkreks/32/40052_2.png) [@tkreks](https://discourse.nodered.org/u/tkreks)\
**Post date:** [6 September 2024 19:58 UTC](https://discourse.nodered.org/t/why-is-the-global-variable-changing/90759/3 "2024-09-06T19:58:18Z")

</div>

Hello! thanks for your reply. If I understood you correctly, when I do msg.b = global.get(2), I get not a new object with the same data, but a reference to the global object? And when I make changes to the object msg.b, am I actually changing the global object as well? Then how can I make sure that the global object does not change without my instructions (global.set)?

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [6 September 2024 20:00 UTC](https://discourse.nodered.org/t/why-is-the-global-variable-changing/90759/4 "2024-09-06T20:00:59Z")

</div>

> [@tkreks](#):
>
> If I understood you correctly, when I do msg.b = global.get(2), I get not a new object with the same data, but a reference to the global object?

Correct!

So do something like

```auto
msg.b = RED.util.cloneMessage(global.get(2))

```

So any (future) changes to `msg.b` are not applied to the global version , but only applied to the cloned copy

---

<div class="post-metadata">

**Author:** ![tkreks](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tkreks/32/40052_2.png) [@tkreks](https://discourse.nodered.org/u/tkreks)\
**Post date:** [6 September 2024 20:02 UTC](https://discourse.nodered.org/t/why-is-the-global-variable-changing/90759/5 "2024-09-06T20:02:40Z")

</div>

I understand you, thank you for your time and for the sample code. Thanks!

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [6 September 2024 20:11 UTC](https://discourse.nodered.org/t/why-is-the-global-variable-changing/90759/6 "2024-09-06T20:11:25Z")

</div>

Note : This only applies to objects  
(references to something that isn't a simple type : `string`, `number` etc)

simple things are `pass-by-val` as opposed to `pass-by-ref`

---

<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:** [20 September 2024 20:11 UTC](https://discourse.nodered.org/t/why-is-the-global-variable-changing/90759/7 "2024-09-20T20:11:35Z")

</div>

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