EN
JavaScript - create variable using user-defined name
0 points
In this article, we would like to show you how to create a variable using a user-defined name in JavaScript.
Quick solution:
xxxxxxxxxx
1
let variableName = 'exampleName';
2
let value = 5;
3
4
this[variableName] = value;
5
6
console.log(this[variableName]);
In this example, we create a variable using a user-defined name with the help of an object.
xxxxxxxxxx
1
let variableName = 'exampleName';
2
let value = 5;
3
4
let object = {
5
[variableName]: value,
6
};
7
8
console.log(object[variableName]);