Thursday, January 26, 2012

eval() and floor() method

eval() Method in JavaScript
1. Evaluates or executes an argument
2. If the argument is an expression, eval() evaluates the expression
3. If the argument is one or more JavaScript statements, eval() executes the statements

Syntax:   eval (string);

JavaScript Code:
<script language="javascript">
function functionName()
{
   var name = 'Anil';
   var grtVar = eval('"My name is: " + name);
   alert(grtVar); 
}
</script>
  
HTML Code:
<button id=”btnEval”  onclick="functionName()">Eval</button>
 
floor() Method in JavaScript

1.      The floor() method rounds a number to the nearest integer, and returns the result.

Syntax: Math.floor(value);

JavaScript Code:
<script language="javascript">
function mathVal(){
   var val = 2.1;
   var state = eval('"Actual value is: " + val +" ;"+ "After truncate value is: " +    Math.floor(val)');
   alert(state); 
}
</script>

HtML Code:
<button id=”btnFloor” onclick="mathVal()">Floor</button>

No comments: