Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$.isNumeric vs. isNaN

Tags:

Is there any difference between $.isNumeric and !isNaN()?

I don't see where they will ever return different results.

like image 683
Phillip Senn Avatar asked Nov 04 '11 16:11

Phillip Senn


People also ask

What does isNaN mean?

Definition and Usage In JavaScript NaN is short for "Not-a-Number". The isNaN() method returns true if a value is NaN. The isNaN() method converts the value to a number before testing it.

What does isNaN check for?

The isNaN() function is used to check whether a given value is an illegal number or not. It returns true if value is a NaN else returns false. It is different from the Number. isNaN() Method. Syntax: isNaN( value )

Is isNaN a Number?

isNaN() is a Number method that checks if a value is NaN (Not A Number). NaN in JavaScript is also a type of Number . The Number. isNaN() method returns true if the value is of the type Number and is equal to NaN .

How can you test if a value is equal to NaN?

The math. isnan() method checks whether a value is NaN (Not a Number), or not. This method returns True if the specified value is a NaN, otherwise it returns False.


2 Answers

From the jQuery blog:

Inside jQuery we’ve found several situations where we need to know if an argument is numeric, or would be successfully converted to a number if it is some other type. We decided to write and document jQuery.isNumeric() since it’s a useful utility. Pass it an argument of any type and it returns true or false as appropriate.


jQuery.isNaN(): This undocumented utility function has been removed. It was confusing because it appropriated the name of a built-in JavaScript function but did not have the same semantics. The new jQuery.isNumeric() serves a similar purpose, but has the benefit of being documented and supported. Despite jQuery.isNaN() being undocumented, several projects on Github were using it. We have contacted them and asked that they use jQuery.isNumeric() or some other solution.

Also see the ticket: http://bugs.jquery.com/ticket/10478


jQuery's isNumeric() checks if a value is a number OR can be converted to a number.

EDIT

To further clarify what isNan() does (and what a NaN value is):

A NaN, which means "Not-a-Number", is classified as a primitive value by the ECMA-262 standard and indicates that the specified value is not a legal number. The function returns true if the argument is not a number and false if the argument is a number.

The classic example of a NaN is zero divided by zero, 0/0

Code:  document.write(isNaN("Ima String"))  document.write(isNaN(0/0))  document.write(isNaN("348"))  document.write(isNaN(348))   Output:  true  true false false 

http://www.devguru.com/technologies/ecmascript/quickref/isnan.html

Semi offtopic, but related is this short talk

like image 143
PeeHaa Avatar answered Sep 21 '22 00:09

PeeHaa


As PeeHaa says, $.isNumeric() checks whether the value is a number or can be converted to a number, isNaN() checks strictly only whether the value is NaN.

Here's a handy comparison chart that uses the jQuery documentation's examples (comparing $.isNumeric() to !isNaN for easier comparison): http://jsfiddle.net/eghE9/

like image 30
JJJ Avatar answered Sep 18 '22 00:09

JJJ