JavaScript eval() Function

Well, this is a javascript function that most of the web programmers must have come across, but still we overlook it. Although this function is very powerful. So lets digg into this function and make our life simpler.

First some Gyan about eval: 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. 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.

eval, evaluates and/or executes a string of JavaScript code that is contained in the codestring argument.

First, eval determines if the argument is a valid string and then parses the string looking for JavaScript code. If there are JavaScript statements in the code, they will be executed and eval will return the value of the last statement (if there is a value). If there is a JavaScript expression, it will be evaluated and its value will be returned.
Note that the codestring argument is optional. However, if there is no argument, eval returned, "undefined".

Exmaple:

In this example we use eval() on some strings and see what it returns:

Code:
eval("vipin=999; vij=777; document.write(vipin + vij);");

Output:
1776

Hmmm, but that not all.... The eval can actually reduce line of codes... look

Reducing the number of lines of code:You can use this command in a variety of activities, for example, if you have a list of fields' names which must contain a value on an HTML form, conventionally you would have had to write an if/then for each field, leading to verbose code, which is not the easiest to read, maintain, or debug, especially within a large and/or complex Web application.

Listing A--Conventional JavaScript

if (document.myForm.field1.value.length==0)

{
    alert("Field 1 requires a value");

}

if (document.myForm.field2.value.length==0)

{
    alert("Field 2 requires a value");

}

if (document.myForm.field3.value.length==0)

{
    alert("Field 3 requires a value");

}

if (document.myForm.field4.value.length==0)

{
    alert("Field 4 requires a value");

}

if (document.myForm.field5.value.length==0)

{
    alert("Field 5 requires a value");

}

if (document.myForm.field6.value.length==0)

{
    alert("Field 6 requires a value");

}

if (document.myForm.field7.value.length==0)

{
    alert("Field 7 requires a value");

}

if (document.myForm.field8.value.length==0)

{
    alert("Field 8 requires a value");

}

Using the EVAL command, you can reduce this to a simple array and loop as shown in below:


var fieldList = new Array('field1','field2','field3','field4','field5','field6','field7','field8');


var tempObj;

for (count=0;count<fieldList.length;count++)

{
    tempObj=EVAL("document.myForm." + fieldList[i]);
    if (tempObj.value.length==0)
    {
        alert(fieldList[i] + " requires a value");
    }

}

As you can see, you have reduced the size of the code required to check the eight fields from 32 to 10 with no loss of functionality, but with a savings of about 70 percent in terms the of number of lines required. Utilizing the EVAL command has also produced a much more user friendly, maintainable and concise code than the traditional approach.

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. Want an example, then put a comment

 

Enjoy.....

Comments

Popular posts from this blog

How does dual SIM mobile work

My First Post

Nokia 7705 Twist