What Does jQuery $.isNumeric Evaluate?
The jQuery $.isNumeric() method evaluates whether its
argument represents a valid, finite numeric value, returning
true for actual numbers and numeric strings, and
false for everything else. This article breaks down how the
function operates under the hood, details the specific inputs it accepts
and rejects, and explains how its evaluation logic differs from native
JavaScript type checking.
How the Evaluation Works
Under the hood, jQuery defines a numeric value as something that can be parsed as a float and is finite. Internally, the evaluation relies on a check equivalent to:
!isNaN(parseFloat(obj)) && isFinite(obj)Because of this implementation, $.isNumeric() checks for
value usability rather than strict JavaScript data types. It tests
whether an input can be safely treated as a number in mathematical
operations.
Values That Evaluate to True
The function returns true for any input that represents
a valid number:
- Integers and Floating-Point Numbers: Native numbers
such as
42,-10,0, and3.14159. - Numeric Strings: String representations of numbers
such as
"42","-10","0", and"3.14159". - Exponential/Scientific Notation: Both numbers and
strings using exponential format, like
1e5,"1e5",2e-3, or"-3e4". - Hexadecimal Numbers: Hexadecimal literals such as
0xFFor their string equivalents like"0xFF". - Leading/Trailing Whitespace with Numbers: Strings
with surrounding spaces that contain only numbers, such as
" 123 ".
Values That Evaluate to False
Unlike the native typeof operator or simple loose
equality checks, $.isNumeric() strictly rejects values that
cannot represent a finite number:
- Empty and Whitespace Strings:
""and" "returnfalse. - Non-Numeric Strings: Any string containing
characters that cannot be fully converted into a number, such as
"abc","12a", or"px10". - Booleans: Both
trueandfalsereturnfalse(unlike native coercion wheretruebecomes1). - Special Number Values:
NaN,Infinity, and-Infinityevaluate tofalse. - Null and Undefined: Both
nullandundefinedreturnfalse. - Arrays and Objects:
[],[1], and{}returnfalse. - Functions and Symbols: Any function reference or
JavaScript Symbol returns
false.
Difference from Native JavaScript Checks
Standard JavaScript methods often produce counterintuitive results when validating numbers:
typeof NaN === "number"evaluates totrue.isNaN("")evaluates tofalsebecause empty strings coerce to0.isNaN(true)evaluates tofalsebecause booleans coerce to1.
jQuery’s $.isNumeric() bypasses these edge cases by
ensuring that empty strings, booleans, and non-finite numbers are
filtered out, making it a reliable way to validate form inputs, API
data, and URL parameters before performing calculations.