# JavaScript: NaN: Not a Number

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

NaN example

"text/javascript">
var a = Math.sqrt(-1); // Return NaN
var b = parseInt("abrakadabra"); // Return NaN
var c = 10.09;

document.write("a = " + a + "
"
);
document.write("b = " + b + "
"
);
document.write("c = " + c + "
"
);

document.write("isNaN(a) = " + isNaN(a) + "
"
); // Return true
document.write("isNaN(b) = " + isNaN(b) + "
"
); // Return true
document.write("isNaN(c) = " + isNaN(c) + "
"
); // Return false

Note
a. If you perform any arithmetic operation with NaN, then the result is NaN.

Previous                                                 Next                                                 Home

This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

# Share the post

JavaScript: NaN: Not a Number

×

# Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

×