How To Use Eval() Function Using JavaScript?

Nov 22, 2021 . Admin



Hello Friends,

Now let's see example of how to use eval() function example. We will check how to use eval() function. This is a short guide on use eval() function in javascript. Here you will learn how to use javascript eval() function. Let's get started with how to use eval() function in javascript.

Here i will give you many example how to use eval() function using javascript.

Example : 1
<html>
    <head>
        <title>How To Use Eval() Function Using JavaScript? - MyWebtuts.com</title>
    </head>
    <body>
        <h3>How To Use Eval() Function Using JavaScript? - MyWebtuts.com</h3>
        <script>
            var x = 8, y = 2, z = 10, a1, a2, a3;
            a1 = eval(" x + y + z ");
            a2 = eval(" x  * y * z");
            a3 = eval(" x  - y");

            document.write(a1 + "<br>");
            document.write(a2 + "<br>");
            document.write(a3);
        </script>
    </body>
</html>  
Output :
20
160
6
Example : 2
<html>
    <head>
        <title>How To Use Eval() Function Using JavaScript? - MyWebtuts.com</title>
    </head>    
    <body>
        <h3>How To Use Eval() Function Using JavaScript? - MyWebtuts.com</h3>
        <script>
            var s1;

            function a1(x, y)
            {
                return x * y;
            }
            eval("s1 = a1(30, 5);");
            document.write(s1);
        </script>
    </body>
</html>  
Output :
150

It will help you...

#Javascript