NaN stands
for Not a Number, it is returned by Javascript when mathematical functions fail
to return the result (or) when a function trying to parse a number fails.
var a =
Math.sqrt(-1); // Return NaN
var b =
parseInt("abrakadabra"); //
Return NaN
Some Points to remember
a. Zero divided by Zero results NaN
b. Infinity divided by Inifnity results
NaN
c. Any Arithmetic operation on
non-numeric operands that can’t be converted to number type leads to NaN.
How to test whether given variables
value is NaN or not?
Javascript
provides special function isNaN(variable), it return true if the given variable
is NaN, else false.
nan.html
<!DOCTYPE html> <html> <head> <title>NaN example</title> </head> <body> <script type="text/javascript"> var a = Math.sqrt(-1); // Return NaN var b = parseInt("abrakadabra"); // Return NaN var c = 10.09; document.write("a = " + a + "<br />"); document.write("b = " + b + "<br />"); document.write("c = " + c + "<br />"); document.write("isNaN(a) = " + isNaN(a) + "<br />"); // Return true document.write("isNaN(b) = " + isNaN(b) + "<br />"); // Return true document.write("isNaN(c) = " + isNaN(c) + "<br />"); // Return false </script> </body> </html>
Note
a. If you
perform any arithmetic operation with NaN, then the result is NaN.
No comments:
Post a Comment