Name
eval( ): execute JavaScript code from a string â ECMAScript v1
Synopsis
eval
(code
)
Arguments
-
code
A string that contains the JavaScript expression to be evaluated or the statements to be executed.
Returns
The value of the evaluated
code
, if any.
Throws
- SyntaxError
Indicates that
code
does not contain legal JavaScript.- EvalError
Indicates that
eval( )
was called illegally, through an identifier other than âevalâ, for example. See the restrictions on this function described in the next section.- Other exception
If the JavaScript code passed to
eval( )
generates an exception,eval( )
passes that exception on to the caller.
Description
eval( )
is a global method
that evaluates a string of JavaScript code in the current lexical
scope. If code
contains an expression,
eval
evaluates the expression and
returns its value. If code
contains a
JavaScript statement or statements, eval(
)
executes those statements and returns the value, if any,
returned by the last statement. If code
does not return any value, eval(
)
returns undefined
.
Finally, if code
throws an exception,
eval( )
passes that exception on
to the caller.
eval( )
provides a very
powerful capability to the JavaScript language, but its use is
infrequent in real-world programs. Obvious uses are to write
programs that act as recursive JavaScript interpreters and to write
programs that dynamically generate and evaluate JavaScript
code.
Most JavaScript functions and methods that expect string arguments accept arguments of other types as ...
Get JavaScript: The Definitive Guide, 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.