# Parametric variable names

**URL:** <https://discourse.nodered.org/t/parametric-variable-names/40034>\
**Category:** General\
**Created:** [30 January 2021 08:45 UTC](https://discourse.nodered.org/t/parametric-variable-names/40034 "2021-01-30T08:45:42Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Simone](https://avatars.discourse-cdn.com/v4/letter/s/cab0a1/32.png) [@Simone](https://discourse.nodered.org/u/Simone)\
**Post date:** [30 January 2021 08:45 UTC](https://discourse.nodered.org/t/parametric-variable-names/40034/1 "2021-01-30T08:45:43Z")

</div>

Hi,  
I would like to dynamically create (and use) variables where the name has an incremental suffix like Var1, Var2, Var3...  
But my code is not working.

```auto
for(var i=0; i<=10; i++) {
    global.set(window['Var'+i],9);   
}

```

---

<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:** [30 January 2021 08:49 UTC](https://discourse.nodered.org/t/parametric-variable-names/40034/2 "2021-01-30T08:49:19Z")

</div>

It would work without the `window` part. What's that about?

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [30 January 2021 09:19 UTC](https://discourse.nodered.org/t/parametric-variable-names/40034/3 "2021-01-30T09:19:47Z")

</div>

Hello,  
You can use [a template string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) to set object properties in context parametrically as the first argument needs to be a string in the context set/get functions:

```auto
for(i=0;i<10;i++){
    global.set(`window.Var${i}`,9);
}

```

This will set the properties Var1, Var2, Var3 and so on of the object window in context. I think this is what you want.

Johannes

---

<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:** [30 January 2021 09:34 UTC](https://discourse.nodered.org/t/parametric-variable-names/40034/4 "2021-01-30T09:34:59Z")

</div>

Would it not be better to use an array? That is what arrays are for.

---

<div class="post-metadata">

**Author:** ![Simone](https://avatars.discourse-cdn.com/v4/letter/s/cab0a1/32.png) [@Simone](https://discourse.nodered.org/u/Simone)\
**Post date:** [30 January 2021 09:54 UTC](https://discourse.nodered.org/t/parametric-variable-names/40034/5 "2021-01-30T09:54:02Z")

</div>

The solution is without windows.

```auto
for(var i=0; i<=10; i++) {
    global.set(['Var'+i],9);   
}

```

Many thanks to all! Sorry for the dummy problem 🙂

---

<div class="post-metadata">

**Author:** ![Simone](https://avatars.discourse-cdn.com/v4/letter/s/cab0a1/32.png) [@Simone](https://discourse.nodered.org/u/Simone)\
**Post date:** [30 January 2021 10:03 UTC](https://discourse.nodered.org/t/parametric-variable-names/40034/6 "2021-01-30T10:03:11Z")

</div>

Colin, you are right but this variables will be arrays. So the best could be array of arrays... too much for me... I've tried before but without success, this solution is the workaround for my dummy skill. If you help me I can jump a step ahead 🙂

---

<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:** [30 January 2021 10:32 UTC](https://discourse.nodered.org/t/parametric-variable-names/40034/7 "2021-01-30T10:32:03Z")

</div>

Arrays of arrays can be confusing. The syntax to access an element is `var[i][j]`.  
You might be better to try the array of arrays route and come back when you have problems. You should end up with a better solution.

> [@Simone](#):
>
> `global.set(['Var'+i],9);`

You don't need the square brackets. In fact I am not sure what effect they have, I am surprised it works.

---

<div class="post-metadata">

**Author:** ![Simone](https://avatars.discourse-cdn.com/v4/letter/s/cab0a1/32.png) [@Simone](https://discourse.nodered.org/u/Simone)\
**Post date:** [6 February 2021 17:26 UTC](https://discourse.nodered.org/t/parametric-variable-names/40034/8 "2021-02-06T17:26:50Z")

</div>

True! Don't needs the square brakets but it works in both ways.

---

<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:** [6 February 2021 21:27 UTC](https://discourse.nodered.org/t/parametric-variable-names/40034/9 "2021-02-06T21:27:51Z")

</div>

What is the point of getting the processor to waste its time building and then interpreting an array when it is not necessary?

---

<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:** [20 February 2021 21:27 UTC](https://discourse.nodered.org/t/parametric-variable-names/40034/10 "2021-02-20T21:27:53Z")

</div>

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