What Types Does Lodash _.isNumber Accept?
The Lodash _.isNumber method is a utility function used
to verify whether a given value qualifies as a number. This guide
outlines the specific types of numbers and representations accepted by
_.isNumber, explores special numeric edge cases like
NaN and Infinity, and highlights common
non-number primitives that the function rejects.
Primitive Numbers
_.isNumber returns true for all standard
JavaScript primitive numeric values. This includes:
- Integers: Positive, negative, and zero (e.g.,
0,-0,42,-100). - Floating-Point Numbers: Decimals of any precision
(e.g.,
3.14,-0.001). - Scientific Notation: Numbers represented using
exponential notation (e.g.,
1e5,2.5e-3). - Non-Decimal Literals: Binary (
0b1010), octal (0o755), and hexadecimal (0xFF) literals, as JavaScript evaluates these as standard numeric primitives at runtime.
Number Objects
Unlike the native JavaScript typeof operator—which
evaluates new Number(x) as an
"object"—Lodash's _.isNumber correctly
identifies Number object instances.
_.isNumber(new Number(42)); // true
typeof new Number(42); // "object"The function checks both whether the value has a typeof
of "number" and whether the internal [[Class]]
or Object.prototype.toString tag matches
[object Number].
Special Numeric Values
JavaScript classifies several special symbolic values under the
Number type according to the IEEE 754 standard.
Consequently, _.isNumber returns true for:
Infinityand-Infinity: Positive and negative infinite values resulting from calculations such as division by zero.NaN(Not a Number): Despite representing an invalid mathematical result,NaNtechnically belongs to the JavaScript number type.
If your use case requires excluding NaN or infinite
numbers, Lodash provides _.isFinite, which validates that a
value is a number while filtering out NaN,
Infinity, and -Infinity.
Values Not Accepted by _.isNumber
To prevent unexpected behavior, _.isNumber returns
false for values that might look like numbers or represent
numbers in other formats:
- Numeric Strings: String representations of numbers
such as
'42','3.14', or'0xFF'returnfalse. - BigInt: Large integer primitives (e.g.,
42norBigInt(42)) belong to the distinctbigintprimitive type and returnfalse. - Other Non-Primitives and Nil Values:
null,undefined,false,true, symbols, arrays, and standard objects all returnfalse.