# Bad form probably, but is this true about context variables

**URL:** <https://discourse.nodered.org/t/bad-form-probably-but-is-this-true-about-context-variables/29006>\
**Category:** General\
**Created:** [26 June 2020 01:52 UTC](https://discourse.nodered.org/t/bad-form-probably-but-is-this-true-about-context-variables/29006 "2020-06-26T01:52:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tmichaeltx](https://avatars.discourse-cdn.com/v4/letter/t/73ab20/32.png) [@tmichaeltx](https://discourse.nodered.org/u/tmichaeltx)\
**Post date:** [26 June 2020 01:52 UTC](https://discourse.nodered.org/t/bad-form-probably-but-is-this-true-about-context-variables/29006/1 "2020-06-26T01:52:13Z")

</div>

When I first started to use context variables, I was only saving primitives and so to change them I had sequences like this:

```auto
flow.set("stored number",1)
//other code
temp = flow.get("stored number")
temp = temp + 1
flow.set("stored number",temp)

```

This would cause the flow variable "stored number" to be created, populated with 1, then populated with 2.

I then learned about javascript passing objects including arrays by reference rather than by value. Thus when working with an object or an array, is the second flow.set redundant? If so, is it bad form to leave it out. Here is the sequence I am thinking:

```auto
flow.set("stored array",[1,2])
//other code
temp = flow.get("stored array")
temp.push(3)
// stored array is already [1,2,3] and so the following line is redundant
flow.set("stored array",temp)

```

I actually ran into problems with the pass by reference when storing objects, but an array was smaller code to show the question.

---

<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:** [26 June 2020 02:57 UTC](https://discourse.nodered.org/t/bad-form-probably-but-is-this-true-about-context-variables/29006/2 "2020-06-26T02:57:48Z")

</div>

Probably it is safer to add the final `flow.set` anyway. As you probably know there are mutating x non-mutating methods array methods. You may get confused if using non-mutating methods, like below:

```auto
flow.set("storedarray",["1","2"]);
//other code
temp = flow.get("storedarray");

node.warn(temp.concat("3"));
node.warn(temp.slice(1));

msg.payload = temp;
return msg;

```

There is a similar discussion here:

> [@Bug updating global arrays](https://discourse.nodered.org/t/bug-updating-global-arrays/2403):
>
> I've been hitting my head against a certain code behavior which I believe`I've captured in the following flow. I'm trying to do something quite simple. I have two global arrays (flow arrays): current\_array and last\_array I want to be able to update the current\_array one element at a time, and at certain times, I would like to copy the current\_array to the last\_array. This works fine when I update the entire array like this: flow.set("current\_array", [1,2,3,4,5,6]); but it fails (sometimes)…

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [26 June 2020 06:23 UTC](https://discourse.nodered.org/t/bad-form-probably-but-is-this-true-about-context-variables/29006/3 "2020-06-26T06:23:16Z")

</div>

Also if you decide at some point to use non-memory based context then the lack of the final set, may also come back to bite you..

---

<div class="post-metadata">

**Author:** ![tmichaeltx](https://avatars.discourse-cdn.com/v4/letter/t/73ab20/32.png) [@tmichaeltx](https://discourse.nodered.org/u/tmichaeltx)\
**Post date:** [26 June 2020 13:00 UTC](https://discourse.nodered.org/t/bad-form-probably-but-is-this-true-about-context-variables/29006/4 "2020-06-26T13:00:05Z")

</div>

This was very valuable to me as it showed how truly complex this area is. In particular, I got a lot of insight out of the youtube video you linked to in that other thread. I am quoting your message in that thread to help others come across it:

> [@Bug updating global arrays](https://discourse.nodered.org/t/bug-updating-global-arrays/2403/5):
>
> For those who want to understand better what it means passing by reference and the shalow copy solution from this post here is a good video: [https://www.youtube.com/watch?v=duyshh9Fs1U](https://www.youtube.com/watch?v=duyshh9Fs1U)

---

<div class="post-metadata">

**Author:** ![tmichaeltx](https://avatars.discourse-cdn.com/v4/letter/t/73ab20/32.png) [@tmichaeltx](https://discourse.nodered.org/u/tmichaeltx)\
**Post date:** [26 June 2020 13:03 UTC](https://discourse.nodered.org/t/bad-form-probably-but-is-this-true-about-context-variables/29006/5 "2020-06-26T13:03:00Z")

</div>

Thanks for providing the additional reason to intentionally save back to the context variable. I hadn't investigated it, but it is good to know that non-memory (disk) based context works differently relative to assignments to other variables.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [26 June 2020 13:44 UTC](https://discourse.nodered.org/t/bad-form-probably-but-is-this-true-about-context-variables/29006/6 "2020-06-26T13:44:59Z")

</div>

non-memory will also include database, cloud, network, and well as disk - ie anything that won't just accept a pointer reference...

---

<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:** [25 August 2020 13:45 UTC](https://discourse.nodered.org/t/bad-form-probably-but-is-this-true-about-context-variables/29006/7 "2020-08-25T13:45:05Z")

</div>

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