How to Use Dynamic Variable Names in JavaScript?
Last Updated :
09 Jan, 2025
Dynamic variable names are variable names that are not predefined but are generated dynamically during the execution of a program. This means the name of a variable can be determined at runtime, rather than being explicitly written in the code.
Here are different ways to use dynamic variables in JavaScript.
1. JavaScript eval() Method
The eval() function in JavaScript evaluates a string as code, allowing dynamic creation of variables. By passing a string to eval() that declares and assigns values to variables, you can dynamically generate variables during runtime within loops or iterations.
Syntax
eval(string);
javascript
let k = 'value';
let i = 0;
for (i = 1; i < 5; i++) {
eval('let ' + k + i + '= ' + i + ';');
}
console.log("value1=" + value1);
console.log("value2=" + value2);
console.log("value3=" + value3);
console.log("value4=" + value4);
Outputvalue1=1
value2=2
value3=3
value4=4
2. Window Object
JavaScript has a global object (window in browsers) where global variables are stored as properties. By assigning values to window['variableName'], you create dynamic global variables. These can be accessed anywhere in the script, allowing flexible manipulation of global variables.
Syntax
window['variableName']
javascript
let i;
for (i = 1; i < 5; i++) {
window['value' + i] = + i;
}
console.log("value1=" + value1);
console.log("value2=" + value2);
console.log("value3=" + value3);
console.log("value4=" + value4);
Output
value1=1
value2=2
value3=3
value4=4
3. Using Map Data Structure
Using the Map data structure in JavaScript is a safe and efficient way to handle dynamic variable names. Maps allow key-value storage with flexible keys, avoiding global scope pollution and providing clear methods (set, get) for managing dynamic variables effectively.
Syntax
let dynamicMap = new Map();
JavaScript
// Create a new Map to store dynamic variables
let dynamicMap = new Map();
// Generate dynamic variable names and assign values
for (let i = 1; i <= 4; i++) {
dynamicMap.set(`value${i}`, i);
}
// Access and print the dynamically created variables
dynamicMap.forEach((value, key) => {
console.log(`${key} = ${value}`);
});
Outputvalue1 = 1
value2 = 2
value3 = 3
value4 = 4
JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.
Similar Reads
How to use JavaScript Variables in Ruby? JavaScript and Ruby are two powerful programming languages used extensively in web development. While each language has its own set of features and capabilities, there are times when developers may need to integrate functionalities from one language into the other. One common scenario is using JavaS
4 min read
How to Convert String to Variable Name in JavaScript? In JavaScript, if we consider the variable names as string values, it is difficult to transform a variableâs name into a string. However, this problem has been handled in many ways for example, static properties of objects, eval(), and modern ways such as Map can be used to achieve the essential eff
3 min read
How to create a variable using a user-defined name in JavaScript ? In this article, we will learn to create a variable using the user-defined name in JavaScript. Variables in JavaScript: Basically, it is a container that holds reusable data in JavaScript. The variable's value can be changed during program execution and should be declared before using it. Before ES6
2 min read
How to Declare Multiple Variables in JavaScript? JavaScript variables are used as container to store values, and they can be of any data type. You can declare variables using the var, let, or const keywords. JavaScript provides different ways to declare multiple variables either individually or in a single line for efficiency and readability.Decla
2 min read
How to create Static Variables in JavaScript ? To create a static variable in JavaScript, you can use a closure or a function scope to encapsulate the variable within a function. This way, the variable maintains its state across multiple invocations of the function. Static keyword in JavaScript: The static keyword is used to define a static meth
2 min read