Skip to Content
JavaScript: The Definitive Guide, 6th Edition
book

JavaScript: The Definitive Guide, 6th Edition

by David Flanagan
May 2011
Intermediate to advanced
1093 pages
40h 54m
English
O'Reilly Media, Inc.
Content preview from JavaScript: The Definitive Guide, 6th Edition

Name

eval() — execute JavaScript code from a string

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

eval() throws a SyntaxError if code is not legal JavaScript code. If the evaluation of code raises an error, eval() propagates that error.

Description

eval() is a global method that evaluates a string of JavaScript code. If code contains an expression, eval evaluates the expression and returns its value. (Some expressions, such as object and function literals look like statements and must be enclosed in parentheses when passed to eval() in order to resolve the ambiguity.) 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() behaves different in ECMAScript 3 and ECMAScript 5, and in ECMAScript 5, it behaves differently in strict mode and non-strict mode, and a minor digression is necessary in order to explain these differences. It is much easier to implement efficient interpreters when a programming language defines eval as an operator instead of as a function. JavaScript’s eval is a function, but for the sake of efficiency, the language draws a distinction between direct, operator-like calls to eval() and ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

JavaScript: The Definitive Guide, 5th Edition

JavaScript: The Definitive Guide, 5th Edition

David Flanagan

Publisher Resources

ISBN: 9781449393854Errata PageSupplemental Content