Below is the example of the eval() function.
- Example:
<script>// JavaScript to illustrate eval() functionfunctionfunc() {// Original stringvara = 4;varb = 4;// Finding the multiplicationvarvalue = eval(newString(a * b));document.write(value);}// Driver codefunc();</script>chevron_rightfilter_none - Output:
16
The eval() function is used to evaluates the expression. If the argument represents one or more JavaScript statements, eval() evaluates the statements. We do not call eval() to evaluate an arithmetic expression.JavaScript evaluates arithmetic expressions automatically.
Syntax:
eval(string)
Parameters: This function accept a single parameter as mentioned above and described below:
- String: A string representing a JavaScript expression, statement, or sequence of statements. The expression can include variables and properties of existing objects.
Return Value: The completion value of evaluating the given code is returned by using eval(). If the completion value is empty, undefined is returned.
Below examples illustrate the eval() function in JavaScript:
Example 1:
Input : eval(new String('2 + 2'));
Output: returns a String object containing "2 + 2"
Input : eval(new String('4 + 4'));
Output: returns a String object containing "4 + 4"
More example codes for the above function are as follows:
Program 1:
<script> // JavaScript to illustrate eval() function function func() { // Original string var a = 2; var b = 2; // Finding the sum var value = eval(new String(a + b)); document.write(value); } // Driver Code func(); </script> |
Output:
4
Program 2:
<script> // JavaScript to illustrate eval() function function func() { // Original string var a var b // Finding the Sumation var value = eval(new String(a + b)); document.write(value); } // Driver code func(); </script> |
Output:
NaN
Supported Browsers:
- Google Chrome
- Internet Explorer
- Mozilla Firefox
- Safari
- Opera
Recommended Posts:
- Is JavaScript's eval() evil?
- PHP | eval() Function
- Node.js REPL (READ, EVAL, PRINT, LOOP)
- JavaScript Course | Understanding Code Structure in JavaScript
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
- JavaScript Course | Loops in JavaScript
- JavaScript Course | Data Types in JavaScript
- JavaScript Course | Printing Hello World in JavaScript
- JavaScript Course | Logical Operators in JavaScript
- JavaScript Course | What is JavaScript ?
- JavaScript Course | Operators in JavaScript
- JavaScript Course | Functions in JavaScript
- JavaScript Course | Variables in JavaScript
- JavaScript Course | Conditional Operator in JavaScript
- JavaScript Course | Objects in JavaScript
- JavaScript Course | JavaScript Prompt Example
- How to include a JavaScript file in another JavaScript file ?
- How to compare two JavaScript array objects using jQuery/JavaScript ?
- How to get the function name from within that function using JavaScript ?
- How to call a function that return another function in JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

