# Using node variables

**URL:** <https://discourse.nodered.org/t/using-node-variables/87533>\
**Category:** General\
**Tags:** function-node\
**Created:** [26 April 2024 20:31 UTC](https://discourse.nodered.org/t/using-node-variables/87533 "2024-04-26T20:31:46Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![mem16421](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@mem16421](https://discourse.nodered.org/u/mem16421)\
**Post date:** [26 April 2024 20:31 UTC](https://discourse.nodered.org/t/using-node-variables/87533/1 "2024-04-26T20:31:46Z")

</div>

Trying to do something that should be straight forward but missing something... basic, and the tutorials I am looking at I feel they are not using variables the way I am doing it in a node

For simplicity I have node where I need to define a starting value once and then increment it. I have it working this way:

On Start  
this.testVar = 100

On Message  
testVar = testVar + 1  
msg = testVar  
return msg;

This works but is global and I only want it to exist in the node, so I read, and it says do this:

On Start  
(nothing)

On Message  
var testVar = context.get('testVar') || 0  
testVar = testVar + 1  
msg = testVar  
return msg;

But this doesn't work as it resets testVar to 0 on each pass (even though it says it shouldn't)

So how would I define a node variable on start, and then use it correctly as it runs?

---

<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:** [26 April 2024 20:42 UTC](https://discourse.nodered.org/t/using-node-variables/87533/2 "2024-04-26T20:42:51Z")

</div>

Hi @mem16421

You are most there, you just need to write back the value (if its not already an object that is setup)

On Message

```auto
let testVar = context.get('testVar') || 0
testVar++
msg.payload = testVar
context.set('testVar', testVar) / * <--- Here */
return msg;

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [26 April 2024 20:45 UTC](https://discourse.nodered.org/t/using-node-variables/87533/3 "2024-04-26T20:45:36Z")

</div>

> [@mem16421](#):
>
> var testVar = context.get('testVar') || 0  
> testVar = testVar + 1  
> msg = testVar  
> return msg;

This is a function node then, and the scope of the context variable is just that node.  
You are never saving the incremented testvar back to context.

```auto
var testVar = context.get('testVar') || 0 // Retrieve the con text variable, or else default to 0
testVar = testVar + 1 // Increment
context.set('testVar', testVar) // Save to context
msg = testVar // This is wrong. Should probably be msg.payload = testVar
return msg;

```

Note that you might have seen an example where a context variable holds an object

```auto
let car = context.get('car') || {"make": "ford", "model": "fiesta"}
car.model = "focus"

```

In this case the change _will_ be saved if the variable exists, without an explicit context.set, though it's probably bad form to rely on that and not call context.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:** [26 April 2024 20:48 UTC](https://discourse.nodered.org/t/using-node-variables/87533/4 "2024-04-26T20:48:34Z")

</div>

All up in my face @jbudd🤣

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [26 April 2024 20:52 UTC](https://discourse.nodered.org/t/using-node-variables/87533/5 "2024-04-26T20:52:51Z")

</div>

Once I starts to type I cannot be distracted...

---

<div class="post-metadata">

**Author:** ![mem16421](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@mem16421](https://discourse.nodered.org/u/mem16421)\
**Post date:** [27 April 2024 11:54 UTC](https://discourse.nodered.org/t/using-node-variables/87533/6 "2024-04-27T11:54:39Z")

</div>

@jbudd @marcus-j-davies

Thank you both!

So if I needed to set an initial value of 100 on testVar I could use On Start for that with a context.set(‘testVar’,100)?

or should On Start just not be used?

---

<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:** [27 April 2024 12:20 UTC](https://discourse.nodered.org/t/using-node-variables/87533/7 "2024-04-27T12:20:22Z")

</div>

> [@mem16421](#):
>
> I could use On Start for that with a context.set(‘testVar’,100)?

Yes, though I tend not to do that for simple cases like this. I tend to forget that On Start is there and when I look at the code in On Message it looks as if the context variable is not initialised, so I usually just use testVar `let testVar = context.get('testVar') ?? 0`

Note that nowadays `let` and `const` are preferred over `var`, google will tell you why if you are interested. Also the newer operator `??` is to be preferred over `||` in this situation as it specifically checks for null or undefined. Keep `||` for boolean expressions.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [27 April 2024 12:26 UTC](https://discourse.nodered.org/t/using-node-variables/87533/8 "2024-04-27T12:26:48Z")

</div>

As far as I can recall I never use On Start for that or anything else.

It makes sense to me for the declaration of a variable to be visible in the same editor window as it's use.

Easy enough for you to try it and report back!

Edit - If you put` context.set(‘testVar’,100)` into On Start, every time a message arrives at the node, testVar will be set to 100, probably defeating the purpose of using a context variable - to save values from one message to the next.

I suppose you could do (not tested)

```auto
const foo = context.get('testVar') ?? 100
context.set('testVar', foo)

```

But I don't see the point.

---

<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:** [27 April 2024 12:44 UTC](https://discourse.nodered.org/t/using-node-variables/87533/9 "2024-04-27T12:44:50Z")

</div>

> [@jbudd](#):
>
> If you put` context.set(‘testVar’,100)` into On Start, every time a message arrives at the node, testVar will be set to 100, probably defeating the purpose of using a context variable - to save values from one message to the next.

I think the whole point of On Start is that it is only executed once, when the node is initially started.

On Message runs every time a message arrives.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [27 April 2024 12:55 UTC](https://discourse.nodered.org/t/using-node-variables/87533/10 "2024-04-27T12:55:00Z")

</div>

Of course you are right.  
I checked my test flow and I seem to have omitted the increment line.

Where's the dunce's hat emoji?

---

<div class="post-metadata">

**Author:** ![mem16421](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@mem16421](https://discourse.nodered.org/u/mem16421)\
**Post date:** [27 April 2024 14:45 UTC](https://discourse.nodered.org/t/using-node-variables/87533/11 "2024-04-27T14:45:54Z")

</div>

Thank you!

---

<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:** [11 May 2024 14:46 UTC](https://discourse.nodered.org/t/using-node-variables/87533/12 "2024-05-11T14:46:39Z")

</div>

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