Home > database >  JavaScript: Is it possible to declare a variable using another variable's value? [duplicate]
JavaScript: Is it possible to declare a variable using another variable's value? [duplicate]

Time:10-07

Here is an example of what I'm trying to accomplish:

const a = 'name';
const ${a} = 1;

The second variable should be:

const name = 1;

Is this possible? Thank you in advance.

CodePudding user response:

Could use an object though, something like

var obj;
var x = "name";
obj[x] = 1;

console.log(obj[x]);

CodePudding user response:

const a = 'name';
eval (a   " = 37");

This will create a variable name with the value 37.

However, I prefer Nobel Eugene's solution as a better approach to the problem.

  • Related