In programming, dynamic variable names don’t have a specific name hard-coded in the script. They are named dynamically with string values from other sources. Dynamic variables are rarely used in JavaScript. But in some cases they are useful. Unlike PHP, there is no special implementation of dynamic variable names in JavaScript. But similar results can be achieved by using some other methods. In JavaScript, dynamic variable names can be achieved by using 2 methods/ways given below.
- eval(): The eval() function evaluates JavaScript code represented as a string in the parameter. A string is passed as a parameter to eval(). If the string represents an expression, eval() evaluates the expression. Inside eval(), we pass a string in which variable valuei is declared and assigned a value of i for each iteration. The eval() function executes this and creates the variable with the assigned values. The code is given below implements the creation of dynamic variable names using eval().
Example:
<script>vark ='value';vari = 0;for(i = 1; i < 5; i++) {eval('var '+ k + i +'= '+ i +';');}console.log("value1="+ value1);console.log("value2="+ value2);console.log("value3="+ value3);console.log("value4="+ value4);</script>chevron_rightfilter_noneOutput:
value1=1 value2=2 value3=3 value4=4
- Window object: JavaScript always has a global object defined. When the program creates global variables they’re created as members of the global object. The window object is the global object in the browser. Any global variables or functions can be accessed with the window object. After defining a global variable we can access its value from the window object. The code given below implements dynamic variable names using the window object. So, the code basically creates a global variable with dynamic name “valuei” for each iteration of i and assigns a value of i to it. Later these variables can be accessed in the script anywhere as they become global variables.
Example:
<script>vari;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);</script>chevron_rightfilter_noneOutput:
value1=1 value2=2 value3=3 value4=4
Recommended Posts:
- How to convert Color Names to Hexcode using JavaScript ?
- How to get the javascript function parameter names/values dynamically?
- variable === undefined vs. typeof variable === “undefined” in JavaScript
- JavaScript | Set object key by variable
- JavaScript | Check the existence of variable
- How to use a variable for a key in a JavaScript object literal?
- JavaScript | Check if a variable is a string
- JavaScript SyntaxError - Missing variable name
- How to check if a variable is an array in JavaScript?
- How to add a property to a JavaScript object using a variable as the name?
- JavaScript TypeError - Variable "x" redeclares argument
- JavaScript SyntaxError "variable" is a reserved identifier
- JavaScript ReferenceError - Assignment to undeclared variable
- How to check a variable is of function type using JavaScript ?
- How to load the contents of a text file into a JavaScript variable?
- How to change the value of a global variable inside of a function using JavaScript ?
- How to insert a JavaScript variable inside href attribute?
- JavaScript ReferenceError - Can't access lexical declaration`variable' before initialization
- How to get names of all the subfolders and files present in a directory using PHP?
- Which characters are valid in CSS class names/selectors?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


