Page 108 - DCAP202_Fundamentals of Web Programming
P. 108
Unit 8: Programming Constructs in JavaScript
Notes
Example: These global built-in functions are explained below with examples.
isNaN()
isNaN() method determines whether value of a variable is a legal number or not.
document.write(isNan(0));
document.write(isNan(“Javascript”));
document.write(isNan(-2.45));
document.write(isNan(“77”));
document.write(isNan(“2012/4/8”));
Results:
false
true
false
false
true
There is a property called NaN of the Number Object which can be used to assign a variable with
‘not a number’ value.
var year= Number.NaN;
document.write(year);
Result:
NaN
isFinite()
As the name indicates, this function is used to find whether a number is a finite legal number.
document.write(isFinite(“5678”));
document.write(isFinite(“ABCD”));
document.write(isFinite(“123_456”));
Result:
true
false
false
eval()
eval() is used to execute Javascript source code. It evaluates or executes the argument passed to
it and generates output.
eval(“var number=2;number=number+2;document.write(number)”);
Result:
4
Number()
Number() method takes an object as an argument and converts it to the corresponding number
value. If the object passed cannot be converted to a number, that is if the object is not in a format
to be represented as a number, then it returns NaN(not a number).
var obj1=new String(“123”);
var obj2=new Boolean(“false”);
LOVELY PROFESSIONAL UNIVERSITY 101