About jQuery $. isNumeric vs. $. isNaN vs. isNaN

Source: Internet
Author: User

In jQuery, there are several ways to determine whether an object is a number or whether it can be converted to a number.
First, jQuery. isNaN () has been removed from the latest version (after 1.7) and replaced by jQuery. isNumeric (). This is not surprising, because jQuery. isNaN () has the same name as the built-in isNaN () in Javascript, but the semantics is not exactly the same, which may cause ambiguity in a certain sense. JQuery. isNumeric () has similar functions and resolves ambiguity issues.

JQuery. isNumeric () checks whether the passed parameter is a number or whether it can be converted to a number. The Javascript built-in isNaN () checks whether the passed parameter is a valid number. A typical example is 0/0.

For specific differences, see test:

Test data:

Copy codeThe Code is as follows:
Var values = [

"-10 ",
,
XFF,

"0xFF ",

"8e5 ",
.. 1415,

+ 10,
,

"",

{},

NaN,

Null,

True,

Infinity,

Undefined,

False

];

Use jQuery verision1.6:

Copy codeThe Code is as follows:
For (var index in values ){

Var v = values [index];

$ ('Table'). append ('<tr> <td>' + v + '</td> <td>'

+ (! IsNaN (v )? "True": "false ")

+ '</Td> <td>'

+ (! $. IsNaN (v )? "True": "false ")

+ '</Td> </tr> ');

}

Output:

 

! IsNaN ()

! $. IsNaN ()

-10

True

True

16

True

True

255

True

True

0xFF

True

True

8e5

True

True

3.1415

True

True

10

True

True

100

True

True

 

True

False

[Object Object]

False

False

NaN

False

False

Null

True

False

True

True

False

Infinity

True

False

Undefined

False

False

Use jQuery version1.7

Copy codeThe Code is as follows:
For (var index in values ){

Var v = values [index];

$ ('Table'). append ('<tr> <td>' + v + '</td> <td>'

+ (! IsNaN (v )? "True": "false ")

+ '</Td> <td>'

+ ($. IsNumeric (v )? "True": "false ")

+ '</Td> </tr> ');

}

Output:

! IsNaN ()

$. IsNumeric ()

-10

True

True

16

True

True

255

True

True

0xFF

True

True

8e5

True

True

3.1415

True

True

10

True

True

100

True

True

 

True

False

[Object Object]

False

False

NaN

False

False

Null

True

False

True

True

False

Infinity

True

False

Undefined

False

False

We can see that $. isNumeric () and! $. IsNaN () results are the same, while jQuery $. isNumeric () comes with Javascript! IsNaN () processes null strings, null, true/false, and Infinity differently. Because isNaN () only checks whether the input value is of the NaN type.

NaN (Not a Number) is a numeric data type, indicating undefined or unrepresentable value, especially the floating point value.

Therefore, isNaN (null) = false is semantically correct, because null is not NaN (in fact, null, true/false, and so on will be converted to the number 0 first ). But if yes! IsNaN () is not appropriate to determine whether the passed value can be converted to a number. Another Javascript method, typeof num = 'number', cannot detect strings. Therefore, if jQuery $. isNumeric () is not used, it is better to rewrite a method to judge, such as using regular expressions or:

Copy codeThe Code is as follows:
Function isNumeric (obj ){
Return! IsNaN (parseFloat (obj) & isFinite (obj );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.