Like the PHP language, JavaScript faces difficulty implementing variable names dynamically. However, the same output can be achieved through multiple strategies. Now, users do not need to hard code the variable name for dynamic purposes. Two auto-generated strategies are introduced, which dynamically modify the variable names during the execution of a program known as eval() and window object.
In this post, a demonstrated overview is provided of how to use dynamic variable names in JavaScript. The outcomes of this post are listed below:
- Using the eval() Method for Dynamic Variable Names
- Using the window Object for Dynamic Variable Names
Method 1: Using the eval() Method for Dynamic Variable Names
The dynamic variable names mean that the names of variables are modified randomly or are user-defined. The eval() method is specifically utilized to evaluate the JavaScript code in a string format. The method takes a string as input in the format of a JavaScript expression. After that, the method evaluates the expression in JavaScript. The developers utilize the for loop to dynamically update the multiple variable names with the eval() method.
Example
An example is provided to implement the eval() method for dynamic variable names. The code is as below.
Code
var a = 'Welcome to JavaScript';
var b = 'a';
console.log(eval(b));
The description of the code is as follows:
In this code, the eval() method is utilized to dynamically update the variable name from a to b. The console.log() method is used to get the specific values.
Output
The output returns the above executable code and displays the dynamic variable names with the eval() method in JavaScript.
Method 2: Using the Window Object for Dynamic Variable Names
In JavaScript, a window object is basically a global object. The object can access any method or global variable in JavaScript code. Moreover, the user can make the dynamic variable with the help of a formatted string. The window object is employed to access global variables or functions in JavaScript code. This strategy is important if the user creates a global variable.
Example
The code is written below to implement the window object for dynamic variable names in JavaScript.
Code
var n1 = 1,
n2 = 2,
n3 = 3;
var x1 = window.n1;
var x2 = window['n1'];
console.log("Welcome to JavaScript");
console.log(x1);
console.log("Welcome to JavaScript World");
console.log(x2);
The description of the above code is listed here.
- The variable n1 is initialized with 1.
- After that, assign the value of n1 to x1 using the dot operator, such as a window.n1 object.
- Similarly, access the value of n2 to x2 using the square bracket [ ].
- Finally, execute the code to display the dynamic variable names in JavaScript.
Output
The output shows the dynamic variable names x1 and x2 are utilized by employing the window object in JavaScript.
Conclusion
Two strategies, the eval() method and window object, are employed to use dynamic variable names in JavaScript. The eval() method takes a string of JavaScript expressions as input and evaluates them. The window object is utilized to access any method and global variables for dynamically updating the variable names. You have learned here to use dynamic variable names using JavaScript. For further understanding, we have provided a set of examples to implement the problem by following eval() and the window object strategy in JavaScript.