What does "isNaN" function do in Java Script? [Resolved]

Posted by Programmer123 under JavaScript on 9/24/2013 | Points: 10 | Views : 2436 | Status : [Member] | Replies : 5
What does "isNaN" function do in Java Script?




Responses

Posted by: Murugavelmsc on: 9/25/2013 [Member] Starter | Points: 50

Up
0
Down

Resolved
The isNaN() function determines whether a value is an illegal number (Not-a-Number).

This function returns true if the value is NaN, and false if not.

Eg:

<script>

document.write(isNaN(123)+ "<br>");
document.write(isNaN(-1.23)+ "<br>");
document.write(isNaN(5-2)+ "<br>");
document.write(isNaN(0)+ "<br>");
document.write(isNaN("Hello")+ "<br>");
document.write(isNaN("2005/12/12")+ "<br>");

</script>

Output:
false
false
false
false
true
true

Regards,
Murugavel S
murugavel.sadagopan@gmail.com
http://murugavelmsc.blogspot.in/

Programmer123, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/25/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
*. The isNaN() function determines whether a value is an illegal number (Not-a-Number).
*. Evaluates an argument to determine if it is not a number.

Syntax :-
isNaN(value)
value = The value you want to evaluate.


Sample:
<html>
<head>
<title>isNaN Fnction</title>

<script language="javascript">
function check()
{
var phone=f1.phone.value;
if(isNaN(phone))
alert("Characters are not allowed in the Phone Number");
else
alert("Your Phone Number is Accepted");
}
</script>

</head>
<body>

<form name="f1">
Phone: <input type="text" name="phone" size="20">
<input type="button" value="check" onclick="check()">
</form>

</body>
</html>


To check that a number is NaN in JavaScript:
isNaN(parseFloat("VariableName"))


If VariableName is float/int then returns true; else false

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Programmer123, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/25/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
isNaN Function
Returns a Boolean value that indicates whether a value is the reserved value NaN (not a number).
isNaN(numValue) 


Return Value: true if the value converted to the Number type is the NaN, otherwise false.

Points:
1) The required numValue is the value to be tested against NaN.
2) You typically use this method to test return values from the parseInt and parseFloat methods.
3) Alternatively, a variable that contains NaN or another value could be compared to itself. If it compares as unequal, it is NaN. This is because NaN is the only value that is not equal to itself.

Examples:

// Returns false.
isNaN(100);

// Returns false.
isNaN("100");

// Returns true.
isNaN("ABC");

// Returns true.
isNaN("10C");

// Returns true.
isNaN("abc123");

// Returns true.
isNaN(Math.sqrt(-1));


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Programmer123, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/25/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
It returns true if the argument is not a number.

B]Example:

CODE]document.write(isNaN("Hello")+ "<br>");
document.write(isNaN("2013/06/23")+ "<br>");
document.write(isNaN(123)+ "<br>");

OutPut:-
true
true
false

Happy Coding,
If it helps you or directs U towards the solution, MARK IT AS ANSWER

Programmer123, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: aswinialuri-19361 on: 9/25/2013 [Member] Starter | Points: 25

Up
0
Down
hi,
NaNfunction-Not a number or illegal number
<script>


document.write(isNaN(0)+ "<br>");

o/p:false

document.write(isNaN("Hello")+ "<br>");

o/p:true

document.write(isNaN("2005/12/12")+ "<br>");

o/p:true
</script>


Mark as Answer if it helps you
Thanks&Regards
Aswini Aluri

Programmer123, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response