# Can you not set context variables by assignment?

**URL:** <https://discourse.nodered.org/t/can-you-not-set-context-variables-by-assignment/13976>\
**Category:** General\
**Created:** [4 August 2019 00:29 UTC](https://discourse.nodered.org/t/can-you-not-set-context-variables-by-assignment/13976 "2019-08-04T00:29:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![higbyk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/higbyk/32/10630_2.png) [@higbyk](https://discourse.nodered.org/u/higbyk)\
**Post date:** [4 August 2019 00:29 UTC](https://discourse.nodered.org/t/can-you-not-set-context-variables-by-assignment/13976/1 "2019-08-04T00:29:57Z")

</div>

I'm using global context variables to store an authorization token, but I've noticed that the value doesn't appear in the context sidebar unless I assign it using a Change node.

In the past I've simply assigned them as such:

```auto
global.auth_token = msg.payload.auth_token;

```

But the documentation seems to require the following:

```auto
global.set("auth_token", msg.payload.auth_token);

```

It seemed circuitous considering it accomplishes the same thing as variable assignment but with more code, so I planned on just sticking with variable assignment, but that doesn't seem to work any more.

What happened to simply assigning context variables? What's the benefit of using set()/get()?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [4 August 2019 01:17 UTC](https://discourse.nodered.org/t/can-you-not-set-context-variables-by-assignment/13976/2 "2019-08-04T01:17:26Z")

</div>

If you were to decide to use file backed context (for persistence), the `global.variable = "something"` method wouldn't work (as the set function does the extra bits like persisting to file).

For consistency sake, I believe we should all be using `global.set(...)`

---

<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:** [4 August 2019 13:57 UTC](https://discourse.nodered.org/t/can-you-not-set-context-variables-by-assignment/13976/3 "2019-08-04T13:57:42Z")

</div>

get/set also allow you to get/update content within an object.

In the future, other persistence capabilities might be added as well and if you use get/set, you will be able to use those - potentially without any change to your code (e.g. if you were to change the default handler in settings.js).

Also, I'm not sure whether you might get some odd behaviours if assigning directly. This would be because JavaScript assigns by _reference_ by default and so you aren't necessarily creating something _new_ when you think you are but only a new reference to something already existing. Using set forces the variable to be something new I believe.
