1) eval is used to evaluate the expression.
Example:
var iValue =10;
document.write(eval(iValue+10));
eval("iFirst=10;iSecond=20;document.write(iFirst*iSecond)");
Output:
20
200
2) eval used to reduce the number of lines.
Suppose you are doing some validation part.
Without eval
if (document.myForm.Name.value.length==0)
{
alert("Name requires a value");
}
if (document.myForm.Salary.value.length==0)
{
alert("Salary requires a value");
}
Using eval
var fieldList = new Array('Name','Salary');
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");
}
}
3) eval can be used to generate dynamic variable.
eval("iValue" + iIndex);
Same way we can use window["iValue"+ iIndex]
I read your post about Java Serialization and its indeed useful man keep it up
ReplyDeleteInterview Questions