Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

eval Function (JavaScript)

Evaluates JavaScript code and executes it.

eval(codeString) 

codeString

Required. A String value that contains valid JavaScript code.

The eval function enables dynamic execution of JavaScript source code.

The codeString string is parsed by the JavaScript parser and executed.

The code passed to the eval function is executed in the same context as the call to the eval function.

Whenever possible, use the JSON.parse function to de-serialize JavaScript Object Notation (JSON) text. The JSON.parse function is more secure and executes faster than the eval function.

The following code initializes the variable myDate to a test date.

var dateFn = "Date(1971,3,8)";
var myDate;
eval("myDate = new " + dateFn + ";");

document.write(myDate);

// Output: Thu Apr 8 00:00:00 PDT 1971

Supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards, Internet Explorer 11 standards. Also supported in Store apps (Windows 8 and Windows Phone 8.1). See Version Information.

Applies To: Global Object (JavaScript)

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft