Lodash _.isNumber() method
What you’ll learn
- How
_.isNumber(value)combinestypeofchecks with boxed Number tagging. - Why
NaNand infinities still register as numbers. - How strings and booleans differ from numeric primitives.
- Which lodash helpers narrow numbers further (
_.isFinite,_.isInteger,_.isNaN).
Prerequisites
You know JavaScript distinguishes number primitives from numeric strings.
- You have seen wrapper objects like
Object(42). - Try-it labs load lodash from the CDN.
Overview
Use _.isNumber as the first gate for math utilities, schema validators, or analytics payloads—then chain tighter guards when NaN or infinity must be excluded.
Primitives
typeof value === "number" covers literals like 3 and -0.
Boxed numbers
[object Number] tagging detects legacy wrappers.
Extremes included
NaN and ±Infinity still count—narrow with _.isFinite.
Syntax
_.isNumber(value) - value: any value to test.
- Returns:
trueif classified as a Number primitive or boxed Number object.
Ordinary numeric primitives
Integers, floats, and tiny magnitudes like Number.MIN_VALUE pass—matching lodash docs.
import isNumber from "lodash/isNumber";
console.log(
"three: " + isNumber(3) + "\n" + // true
"minVal: " + isNumber(Number.MIN_VALUE) // true
); Infinity and NaN
Lodash follows ECMAScript: non-finite values are still typeof number—use _.isFinite when you need finite math only.
import isNumber from "lodash/isNumber";
console.log(
"infinity: " + isNumber(Infinity) + "\n" + // true
"nan: " + isNumber(NaN) // true
); Numeric strings versus boxed numbers
Strings fail until coerced; boxed Number objects satisfy the tag check.
import isNumber from "lodash/isNumber";
console.log(
"numericStr: " + isNumber("3") + "\n" + // false
"boxed: " + isNumber(Object(42)) // true
); 📋 _.isNumber vs related checks
| API | Behavior |
|---|---|
_.isNumber(x) | Number primitive or boxed Number—including NaN and ±Infinity. |
_.isFinite(x) | Requires finite primitive number (filters NaN and infinities). |
_.isInteger(x) | Finite integer primitives only—no strings or boxed numbers. |
typeof x === "number" | Matches primitives but misses Object(7). |
Pitfalls to avoid
Query strings
HTTP parameters arrive as strings—parse before expecting _.isNumber to pass.
Arbitrary precision
BigInt literals are not IEEE doubles—lodash intentionally returns false.
NaN propagation
_.isNumber(NaN) is true; combine with _.isNaN when NaN must be rejected.
❓ FAQ
Summary
- Purpose: classify Number primitives and boxed numbers consistently.
- Remember: widen or narrow with
_.isFinite,_.isInteger, or_.isNaNas needed. - Next: explore more on Lodash _.isObject().
Lodash treats NaN and Infinity as numbers because typeof reports "number"—reach for _.isFinite when you need real finite arithmetic values.
6 people found this page helpful
