Core JavaScript 1.5 Reference:Global Functions:eval
From MDC
Contents |
[edit] Summary
Core Function
Evaluates a string of JavaScript code without reference to a particular object.
[edit] Syntax
eval(string[, object])
[edit] Parameters
-
string - A string representing a JavaScript expression, statement, or sequence of statements. The expression can include variables and properties of existing objects.
-
object - An optional argument; if specified, the evaluation is restricted to the context of the specified object.
[edit] Description
eval is a top-level function and is not associated with any object.
The argument of the eval function is a string. If the string represents an expression, eval evaluates the expression. If the argument represents one or more JavaScript statements, eval performs the statements. Do not call eval to evaluate an arithmetic expression; JavaScript evaluates arithmetic expressions automatically.
If you construct an arithmetic expression as a string, you can use eval to evaluate it at a later time. For example, suppose you have a variable x. You can postpone evaluation of an expression involving x by assigning the string value of the expression, say "3 * x + 2", to a variable, and then calling eval at a later point in your script.
If the argument of eval is not a string, eval returns the argument unchanged. In the following example, the String constructor is specified, and eval returns a String object rather than evaluating the string.
eval(new String("2 + 2")); // returns a String object containing "2 + 2"
eval("2 + 2"); // returns 4
You can work around this limitation in a generic fashion by using toString.
var expression = new String("2 + 2");
eval(expression.toString());
You cannot indirectly use the eval function by invoking it via a name other than eval; if you do, a runtime error might occur. For example, you should not use the following code:
var x = 2;
var y = 4;
var myEval = eval;
myEval("x + y");
You should not use eval to convert property names into properties. Consider the following example. The getFieldName(n) function returns the name of the specified form element as a string. The first statement assigns the string value of the third form element to the variable field. The second statement uses eval to display the value of the form element.
var field = getFieldName(3);
document.write("The field named ", field, " has value of ",
eval(field + ".value"));
However eval is not necessary here. In fact, its use here is discouraged. Instead, use the member operators, which are much faster:
var field = getFieldName(3);
document.write("The field named ", field, " has value of ",
field[value]);
[edit] Backward Compatibility
[edit] JavaScript 1.3 and earlier
You can use eval indirectly, although it is discouraged.
[edit] JavaScript 1.1
eval is also a method of all objects.
[edit] Examples
The following examples display output using document.write. In server-side JavaScript, you can display the same output by calling the write function instead of using document.write.
[edit] Example: Using eval
In the following code, both of the statements containing eval return 42. The first evaluates the string "x + y + 1"; the second evaluates the string "42".
var x = 2;
var y = 39;
var z = "42";
eval("x + y + 1"); // returns 42
eval(z); // returns 42
[edit] Example: Using eval to evaluate a string of JavaScript statements
The following example uses eval to evaluate the string str. This string consists of JavaScript statements that open an Alert dialog box and assign z a value of 42 if x is five, and assigns 0 to z otherwise. When the second statement is executed, eval will cause these statements to be performed, and it will also evaluate the set of statements and return the value that is assigned to z.
var str = "if (x == 5) {alert('z is 42'); z = 42;} else z = 0; ";
document.write("<P>z is ", eval(str));
[edit] Return value
eval returns the value of the last expression evaluated.
var str = "if ( a ) { 1+1; } else { 1+2; }";
var a = true;
var b = eval(str); // returns 2
alert("b is : " + b);
a = false;
b = eval(str); // returns 3
alert("b is : " + b);