You are on page 1of 1

JavaScript - Evaluate An Expression

<HTML>
<HEAD> <TITLE> Evaluate An Expression using the eval() function </TITLE>
</HEAD>

<BODY BGCOLOR = "skyblue">


<!-- The JS eval() function accepts a single string that contains the code to be
executed. It is a top-level function, and it is not applied to an instance using the
dot (.) operator.
-->

<!-- Try with following expressions also:


Math.sqrt(16)
2*30/5*(19 - 6)
Math.sin(3.142 * 30/180)
1/0
-->

<SCRIPT LANGUAGE = JavaScript>

var exprn, value; //exprn contains the expression entered


// value contains the value of that exprn

exprn = prompt("Evaluate an expression", "Enter any expression");

value = eval(exprn);

document.writeln("The value of the expression " + exprn + " is " + value);

</SCRIPT>

</BODY>
</HTML>

You might also like