| Core function | |
| Implemented in | Navigator 2.0 |
Syntax
eval(string)
Parameters
string | A string representing a JavaScript expression, statement, or sequence of statements. The expression can include variables and properties of existing objects. |
Description
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.
eval is also a method of all objects. This method is described for the Object class.
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.
Example 1. Both of the write statements below display 42. The first evaluates the string "x + y + 1"; the second evaluates the string "42".
var x = 2
Example 2. In the following example, the
var y = 39
var z = "42"
document.write(eval("x + y + 1"), "<BR>")
document.write(eval(z), "<BR>")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)
Example 3. The following example uses
document.write("The field named ", field, " has value of ",
eval(field + ".value"))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; "
Example 4. In the following example, the
document.write("<P>z is ", eval(str))setValue function uses eval to assign the value of the variable newValue to the text field textObject:
function setValue (textObject, newValue) {
Example 5. The following example creates
eval ("document.forms[0]." + textObject + ".value") = newValue
}breed as a property of the object myDog, and also as a variable. The first write statement uses eval('breed') without specifying an object; the string "breed" is evaluated without regard to any object, and the write method displays "Shepherd", which is the value of the breed variable. The second write statement uses myDog.eval('breed') which specifies the object myDog; the string "breed" is evaluated with regard to the myDog object, and the write method displays "Lab", which is the value of the breed property of the myDog object.
function Dog(name,breed,color) {
this.name=name
this.breed=breed
this.color=color
}
myDog = new Dog("Gabby")
myDog.breed="Lab"
var breed='Shepherd'
document.write("<P>" + eval('breed'))
document.write("<BR>" + myDog.eval('breed')) See also
Object.eval method
Last Updated: 10/31/97 16:38:00