Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: Server sided dynamic variable names

How would I create dynamic variable names in NodeJS? Some examples say to store in the window variable, but I was assuming that is client-side Javascript. Correct me if I'm wrong.

like image 877
hexacyanide Avatar asked Sep 26 '12 00:09

hexacyanide


People also ask

Can we create dynamic variable in JavaScript?

Use an Array of VariablesThe simplest JavaScript method to create the dynamic variables is to create an array. In JavaScript, we can define the dynamic array without defining its length and use it as Map. We can map the value with the key using an array and also access the value using a key.

What is a dynamic variable JavaScript?

Dynamic variables are those types of variables that don't have any specific name in the code through hard coded. A dynamic variable name is auto-generated while running the program. It is possible to create a dynamic variable in JavaScript and then using it for a specific task.

How do you concatenate a variable name in JavaScript?

In JavaScript, we can assign strings to a variable and use concatenation to combine the variable to another string. To concatenate a string, you add a plus sign+ between the strings or string variables you want to connect. let myPet = 'seahorse'; console.


1 Answers

Generally you would do something like:

var myVariables = {};
var variableName = 'foo';

myVariables[variableName] = 42;
myVariables.foo // = 42
like image 136
Bill Avatar answered Oct 01 '22 20:10

Bill