About

Translate

Jan 11, 2010

Javescript- The eval function & Conversion function

The eval() function has many uses in sophiscated programming techniques. rather than looking for a number in a string, eval() looks for any valid javascripts expression. A simple example assigns 25 to the a variable
a=eval("20+1+4");
The use of eval() can be much more complicated than this. you can use any javascript expression in the string passed to eval() numeric opertions, comparision operation, statement, multiple statements, and even entire javascripts function. for example the following statements define a variable called fred and set it's value to 31. the value of the text variable is used as the variable's name:
var text="fred";
var statement=text +"=31";
eval(statement);
because() can work with any expression, you can use it to perform calculation based on data entered by the user.
Conversion function
The parseInt function:- the parseInt() function looks for an integer number as the first part of the string. it ignores the decimal portion, if found. for example, this statement assigns the variable a to the value 39;
a=parseInt("39 steps");

the parseFloat function:- the parseFloat() function is similar to parseInt(), but works with floating-point values. It attempts to find a decimal floating-point number in the string, and returns it. for example, following statements assign the value 2.7178 to the variable a:
a="2.7178 is the base of a nutural logarithm.";
Reactions:

0 comments:

Post a Comment