# Increment a global variable with a change node

**URL:** <https://discourse.nodered.org/t/increment-a-global-variable-with-a-change-node/44893>\
**Category:** General\
**Created:** [28 April 2021 20:45 UTC](https://discourse.nodered.org/t/increment-a-global-variable-with-a-change-node/44893 "2021-04-28T20:45:54Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![augjoh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/augjoh/32/26464_2.png) [@augjoh](https://discourse.nodered.org/u/augjoh)\
**Post date:** [28 April 2021 20:45 UTC](https://discourse.nodered.org/t/increment-a-global-variable-with-a-change-node/44893/1 "2021-04-28T20:45:54Z")

</div>

Hello,

is it possible to replace a function node containing the following code, with a change node?

```auto
var metric = global.get('metric');
metric++;
global.set('metric', metric);

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [28 April 2021 20:53 UTC](https://discourse.nodered.org/t/increment-a-global-variable-with-a-change-node/44893/2 "2021-04-28T20:53:08Z")

</div>

Yes that's possible'  
Set Global metric  
JSONata expression  
`$globalContext("metric") + 1`

---

<div class="post-metadata">

**Author:** ![augjoh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/augjoh/32/26464_2.png) [@augjoh](https://discourse.nodered.org/u/augjoh)\
**Post date:** [28 April 2021 21:06 UTC](https://discourse.nodered.org/t/increment-a-global-variable-with-a-change-node/44893/3 "2021-04-28T21:06:59Z")

</div>

Thanks! Works great!

---

<div class="post-metadata">

**Author:** ![duaw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/duaw/32/20970_2.png) [@duaw](https://discourse.nodered.org/u/duaw)\
**Post date:** [29 April 2021 10:26 UTC](https://discourse.nodered.org/t/increment-a-global-variable-with-a-change-node/44893/4 "2021-04-29T10:26:57Z")

</div>

> [@E1cid](#):
>
> globalContext

Amazing!  
Where in the docs would you find that precious little gem?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [29 April 2021 11:30 UTC](https://discourse.nodered.org/t/increment-a-global-variable-with-a-change-node/44893/5 "2021-04-29T11:30:22Z")

</div>

> [@duaw](#):
>
> Amazing!  
> Where in the docs would you find that precious little gem?

IT HELPS TO READ THE DOCUMENTATION:  
[https://nodered.org/docs/user-guide/context](https://nodered.org/docs/user-guide/context)

---

<div class="post-metadata">

**Author:** ![duaw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/duaw/32/20970_2.png) [@duaw](https://discourse.nodered.org/u/duaw)\
**Post date:** [29 April 2021 12:42 UTC](https://discourse.nodered.org/t/increment-a-global-variable-with-a-change-node/44893/6 "2021-04-29T12:42:42Z")

</div>

Of course, I do read the docs and I use that valuable information mentioned there.  
However, this JSONata magic

> [@E1cid](#):
>
> JSONata expression  
> `$globalContext("metric") + 1`

was new to me! I read and re-read that piece of documentation, but I cannot find that JSONata magic! (Or do I miss something? Or can I just not connect the dots?)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [29 April 2021 12:46 UTC](https://discourse.nodered.org/t/increment-a-global-variable-with-a-change-node/44893/7 "2021-04-29T12:46:44Z")

</div>

Also if you look at the Jsonata expression editor all $functions are listed there, with their parameters.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [29 April 2021 14:09 UTC](https://discourse.nodered.org/t/increment-a-global-variable-with-a-change-node/44893/8 "2021-04-29T14:09:23Z")

</div>

In case you did not realise, JSONata is just a tool that is included in node-red. If you want a full explanation you need to look in the JSONata documentation.

---

<div class="post-metadata">

**Author:** ![augjoh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/augjoh/32/26464_2.png) [@augjoh](https://discourse.nodered.org/u/augjoh)\
**Post date:** [8 May 2021 05:53 UTC](https://discourse.nodered.org/t/increment-a-global-variable-with-a-change-node/44893/9 "2021-05-08T05:53:39Z")

</div>

I agree, that a complete Jsonata re-documentation would be out of scope for Node-RED. IMHO I would like to see the example above, how Jsonata can interact with messages, or context inside the Cookbook ([https://cookbook.nodered.org/](https://cookbook.nodered.org/)). It would be a good starting point to allude users to this powerful built-in.

---

<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 May 2021 05:54 UTC](https://discourse.nodered.org/t/increment-a-global-variable-with-a-change-node/44893/10 "2021-05-22T05:54:21Z")

</div>

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