• Call us: +977-980 116102

The power of JavaScript’s EVAL command

The JavaScript EVAL command can be very powerful when using dynamic content in Web-based applications. EVAL can reduce code and eases interaction with data that is unknown at load time, but there are drawbacks to consider.

Many JavaScript programmers have come across the EVAL command, as it has been around since JavaScript 1.0 and ECMA-262, but few will have used it. In this article, I’ll take a quick look at several potential uses for this command, including one allowing us to write code that is easier to read, maintain, and has fewer lines while achieving the same effect. I will also look at how EVAL can assist you when you are using dynamic content in your Web-based applications.

The command syntax for the EVAL command is:
EVAL (“”);

What does EVAL do
The command executes the JavaScript contained within the brackets, in some cases, this may return a value which can be assigned to a variable. But, how does this help JavaScript programmers in their day-to-day work?

As EVAL allows you to dynamically create and execute JavaScript, you can use it to reduce the number of lines of code you need to use; you can also use it to create JavaScript to perform activities that would be significantly more difficult without using the command. In many cases, you can move this functionality out of a single page and convert it into a generic function that you can reuse thanks to the EVAL function.

The real power of EVAL
The real power of this command is its ability to work with data that is not known at load time—for example, user-entered data. As I have mentioned, EVAL takes any string that you provide and executes it returning a result, if one is applicable. You can use this to execute JavaScript where what you are being asked to execute is not known at load time. For example, if we have a calculation script that takes an equation provided by the user and returns a result, you can use EVAL to perform the calculation.
A word of caution
Despite the advantages of using EVAL, in some cases, the only major problem is the command itself. It will execute whatever is passed to it, so be very wary of using this command in a situation where you don’t trust the users’ honesty or ability completely.

Browser support
As this command has been around since JavaScript 1.0, it is supported in all browsers, so there should be no problems in using it. The only browser-specific issue which you may face will be the result of the code that you pass into the EVAL command itself.

Using the command
This command has power potential and is used/misused in many Web sites and books. There appears to be a lot of discussion in the JavaScript community concerning the correct use of this command ranging from the “thou shalt never use EVAL” to “there’s more than one way to do it” camps.

Yashu Sthapit

it's me...........

Post a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.