eval

Evaluates a string of MetaScript code without reference to a particular object.

Core function

Syntax

eval( string )

Parameters

Parameter Description
string A string representing a MetaScript expression, statement, or sequence of statements. The expression can include variables and properties of existing objects.

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 MetaScript statements, eval performs the statements. Do not call eval to evaluate an arithmetic expression; MetaScript 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.

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:

Examples

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".

See also

Object.eval method

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.