# Variable in function that keeps its value between each iteration

**URL:** <https://discourse.nodered.org/t/variable-in-function-that-keeps-its-value-between-each-iteration/921>\
**Category:** General\
**Created:** [19 June 2018 11:50 UTC](https://discourse.nodered.org/t/variable-in-function-that-keeps-its-value-between-each-iteration/921 "2018-06-19T11:50:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hary/32/12340_2.png) [@hary](https://discourse.nodered.org/u/hary)\
**Post date:** [19 June 2018 11:50 UTC](https://discourse.nodered.org/t/variable-in-function-that-keeps-its-value-between-each-iteration/921/1 "2018-06-19T11:50:16Z")

</div>

Hi, it’s me again !

I’ve seen somewhere how to do to keep the value of a variable between each pass of the function (like a counter for instance).  
I think it has to do with “global” or “context” or maybe both. But impossible to find this information back !  
I think it was something like:

```auto
global.context myvar = 0;
myvar += 1;
```

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [19 June 2018 11:54 UTC](https://discourse.nodered.org/t/variable-in-function-that-keeps-its-value-between-each-iteration/921/2 "2018-06-19T11:54:37Z")

</div>

The documentation covers this here: [https://nodered.org/docs/writing-functions#storing-data](https://nodered.org/docs/writing-functions#storing-data)
