Using JavaScript dynamic variable names

In programming, dynamic variable names do not have specific names hardcoded in scripts. They are dynamically named using 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 obtained by using some other methods. In JavaScript, dynamic variable names can be implemented using the 2 methods/way given below.

eval(): The eval() function evaluates the JavaScript code represented as a string in the argument. Strings are passed as arguments to eval(). If the string represents an expression, eval() evaluates the expression. In eval(), we pass a string in which the variable valuei is declared and the value of i is assigned for each iteration. The eval() function does this and creates a variable with the assigned value. The code given below implements the use of eval() to create dynamic variable names.

example:

 <script> var k = 'value'; var i = 0; for(i = 1; i < 5; i++)

The post Using JavaScript Dynamic Variable Names first appeared on Lenix Blog .

This article is reprinted from https://blog.p2hp.com/archives/8696
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment