javaScript eval()

--The eval() function in JavaScript 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.

--The completion value of evaluating the given code is returned by using eval(). If the completion value is empty, undefined is returned.



syntax:-

eval(string);

String:- A string represents JavaScript expression, statement, or sequence of statements. The expression can be variables and properties of existing objects.

Example:-

<html>  
<head>  
<script>  
var a = 10b = 20c = 30, sum, mul, sub;  
sum = eval(" a + b + c ");  
mul = eval(" a  * b * c");  
sub = eval(" a  - b");  
document.write(sum + "<br>");  
document.write(mul + "<br>");  
document.write(sub);  
</script>  
</head>  
<body>  
</body>  
</html>  

Output:-

sum 50
mul 6000
sub -10

Post a Comment

0 Comments