Permalink
Fetching contributors…
Cannot retrieve contributors at this time
69 lines (55 sloc) 2.03 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic f1_keywords dev_langs helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
eval Function (JavaScript) | Microsoft Docs
01/18/2017
windows-client-threshold
devlang-javascript
language-reference
eval
JavaScript
TypeScript
DHTML
eval function
parsing, code
parser
85587e39-9325-4b75-b9f9-9d7d475a2120
21
mikejo5000
mikejo
ghogen

eval Function (JavaScript)

Evaluates [!INCLUDEjavascript] code and executes it.

Syntax

eval(codeString)   

Parameters

codeString
Required. A String value that contains valid [!INCLUDEjavascript] code.

Remarks

The eval function enables dynamic execution of [!INCLUDEjavascript] source code.

The codeString string is parsed by the [!INCLUDEjavascript] 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.

Example

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  

Requirements

[!INCLUDEjsv1]

Applies To: Global Object

See Also

String Object