In jquery, there are several ways to determine whether an object is a number or if it can be converted to a number.
First, Jquery.isnan () has been removed (after 1.7) in the latest version and replaced by Jquery.isnumeric (). This is not surprising, because Jquery.isnan () is the same as the JavaScript built-in isNaN () name, but the semantics are not exactly the same, in a certain sense can cause ambiguity. Jquery.isnumeric () has its similar function, and also solves the ambiguity problem.
Jquery.isnumeric () checks whether the passed arguments are numbers or can be converted to numbers; Javascript's built-in isNaN () checks whether the incoming parameters are a valid number, typically 0/0.
Specific differences can be seen in the test:
Test data:
Copy Code code as follows:
var values = [
"-10",
,
Xff,
"0xFF",
"8e5",
.1415,
+10,
,
"",
{},
NaN,
Null
True
Infinity,
Undefined
False
];
Using jquery verision1.6:
Copy code code as follows:
for (var index in values) {
var v = values[I Ndex];
$ (' 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 |
Using JQuery version1.7
Copy Code code as follows:
for (var index in values) {
var v = values[I Ndex];
$ (' 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 |
As you can see, $.isnumeric () is the same as the!$.isnan () result, and the JQuery $.isnumeric ()!isnan () with JavaScript is in the empty string, NULL, True/false, The processing of infinity is different. Because isNaN () simply checks whether the passed-in value is a Nan type.
NaN (not a number) is a numeric data type that indicates a value that is undefined (undefined) or cannot be represented (unrepresentable), especially the floating-point value.
Therefore, isNaN (null) = = False is semantically correct because null is not Nan (in fact null, Ture/false, and so on) is first converted to the number 0. But if you use it! isNaN () to determine whether the incoming value can be converted to a number, and not appropriate. And JavaScript is another way typeof num = ' number ' cannot detect string conditions. Therefore, if you do not use JQuery $.isnumeric (), it is best to rewrite a method judgment, such as using a regular to determine or:
Copy Code code as follows:
function IsNumeric (obj) {
Return!isnan (parsefloat (obj)) && isfinite (obj);
}