JavaScript > Global Reference

Evaluate in JavaScript

How to evaluate or execute an argument in JavaScript?

By using eval() function we can evaluate or execute an argument.

<p>Click the below button to Evaluate.</p>
<input type="button" onclick="mySearch()" value="Evaluate">
<p id="myId"></p>

    function mySearch() {
        var a = 5;
        var b = 10;
        var p = eval("a * b") + "</br>";
        var q = eval("a + b") + "</br>";
        var r = eval("b / a") + "</br>";

        var s = p + q + r;
        document.getElementById("myId").innerHTML = s;

In the above code snippet we have given Id as "myId"to the second <p> element in the HTML code. There is a function mySearch() in the script code which is connected to the onclick of the HTML button.

There are five variable, in that a & b are with values "5 & 10", p,q & r with the eval() function.

Onclick of the button "Evaluate" in the HTML code fires the function mySerach() in the script code at the same time eval() function evaluates or executes the given arguments according to the input values and returns the output.


 Views: 4019 | Post Order: 180

Write for us

Hosting Recommendations