< Continued from page 4
The fifth worst Javascript coding used by Javascript novices is to use the eval() method to evaluate variable names. The eval method is just about the least efficient statement that you can use in Javascript and most of the time its use in completely unnecessary.
The most common usage that people make is something along the lines of eval('myVar' + i) in order to generate a variable name that has a variable portion in the name.
Exactly the same variable can be referenced using window['myVar' + i] because all Javascript variables are automatically assigned as properties of the window object. Referencing the variables from the window object is much more efficient than using eval.
The use of eval should be reserved for those very rare instances where there is not any alternative way to produce the required result.
previous post
next post