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:
// ONLINE-RUNNER:browser;
let variableName = 'exampleName';
let value = 5;
this[variableName] = value;
console.log(this[variableName]);
Practical example
In this example, we create a variable using a user-defined name with the help of an object.
// ONLINE-RUNNER:browser;
let variableName = 'exampleName';
let value = 5;
let object = {
[variableName]: value,
};
console.log(object[variableName]);