Home
Do you know not to use the "eval" function?
  v2.0 Posted at 24/08/2015 11:08 AM by William Yin

The Javascript command "eval" evaluates the content of a text string and then runs it as Javascript code. It's common to see it around, however "eval" is one of the most inefficient constructs that JavaScript has. There are always more efficient ways to code and get a direct reference.

Most of people that use "eval" want part of a variable name to be variable.

Once you realize that all global variables are held in the window array it becomes rather obvious that you can refer to that same field name without needing to use "eval" by referring to it.

eval('somevar' + num)

Figure: Bad example - The developer creates the variable name by concatenating the constant and variable parts together

window['somevar' + num]

Figure: Good example - Referencing the same field is as simple to code and more efficient than using "eval"

Related rules

    Do you feel this rule needs an update?

    If you want to be notified when this rule is updated, please enter your email address:

    Comments: