Pages

Wednesday, May 6, 2009

Javascript eval()

The eval() function evaluates a string and executes it as if it was script code.

Say you have 10 span tags each named "myspan_"+ <>, and the numbers range from 0 to 9. If you want to call a function on each of them, you do not have to send the full name or search for the array of span tags and figure out what one you want. Just send the number to a function and use eval("myspan_"+number+...) to perform your desired task.

You should avoid using eval() as much as possible, but in some scenarios its simpler and elegant to use it.

More details : Mozilla Developer center