1. Home
  2. Computing & Technology
  3. JavaScript

Five Most Common Coding Errors
eval

By , About.com Guide

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.

Explore JavaScript
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. JavaScript
  4. Javascript Tutorials
  5. eval>

©2009 About.com, a part of The New York Times Company.

All rights reserved.