The Wayback Machine - https://web.archive.org/web/20230512160218/https://www.geeksforgeeks.org/javascript-eval-function/
Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

JavaScript eval() Function

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The Javascript  eval() function is used to evaluate 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. 

Note:

eval(): This function was used to evaluate a string as JavaScript code, but it is now considered deprecated because it can introduce security vulnerabilities and is often unnecessary.

Syntax:

eval(string)

Parameters: This function accepts 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. 

Example:

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"

The below examples illustrate the eval() function in JavaScript: 

Example 1: 

Javascript




<script>
    // JavaScript to illustrate eval() function
    function func() {
     
        // Original string
        var a = 4;
        var b = 4;
     
        // Finding the multiplication
        var value = eval(new String(a * b));
        console.log(value);
    }
    // Driver code
    func();
</script>

Output:

"16"

Example 2: 

Javascript




<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));
        console.log(value);
    }
    // Driver Code
    func();
</script>

Output:

"4"

Example 3

Javascript




<script>
    // JavaScript to illustrate eval() function
    function func() {
     
        // Original string
        var a
        var b
     
        // Finding the Summation
        var value = eval(new String(a + b));
        console.log(value);
    }
    // Driver code
    func();
</script>

Output:

NaN

We have a complete list of Javascript Function methods, to check those please go through this Javascript Function Complete reference article.

Supported Browsers:

  • Google Chrome
  • Internet Explorer
  • Mozilla Firefox
  • Safari
  • Opera

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.


My Personal Notes arrow_drop_up
Last Updated : 13 Jan, 2023
Like Article
Save Article
Similar Reads
Related Tutorials