Lodash _.isFinite() method
What you’ll learn
- How
_.isFinite(value)restricts inputs to primitive numbers. - Why
Infinity,-Infinity, andNaNfail the check. - How lodash differs from legacy global
isFinitecoercion. - Why boxed numbers and numeric strings do not pass.
Prerequisites
Comfort with JavaScript number literals and the difference between primitives and objects.
- You know
InfinityandNaNare special IEEE-754 values. - Try-it labs load lodash from the CDN.
Overview
Reach for _.isFinite after parsing unknown inputs—guarding sliders, pagination math, canvas transforms, or analytics counters—without accidentally accepting coerced strings.
Primitive-only
typeof gate keeps strings and objects out.
No coercion
Unlike global isFinite, lodash will not cast numeric text.
Reject extremes
Infinity and NaN fail native finite checks.
Syntax
_.isFinite(value) - value: any value to test.
- Returns:
trueifvalueis a finite primitive number; otherwisefalse.
Ordinary finite numbers
Integers and fractional literals both qualify when they stay within finite IEEE bounds.
import isFinite from "lodash/isFinite";
console.log(
"finiteInt: " + isFinite(42) + "\n" + // true
"finiteFloat: " + isFinite(-12.875) // true
); Infinity, NaN, and numeric strings
Non-finite math values fail; string digits fail because lodash refuses coercion.
import isFinite from "lodash/isFinite";
console.log(
"posInfinity: " + isFinite(Infinity) + "\n" + // false
"nanVal: " + isFinite(NaN) + "\n" + // false
"numericStr: " + isFinite("99") // false
); Primitive versus boxed number
Coerce explicitly with Number(...) if APIs might hand back object wrappers.
import isFinite from "lodash/isFinite";
console.log(
"primitiveNum: " + isFinite(7) + "\n" + // true
"boxedNum: " + isFinite(Object(7)) // false
); 📋 _.isFinite vs related checks
| API | Matches |
|---|---|
_.isFinite(x) | Finite primitive numbers only. |
Number.isFinite(x) | Nearly identical semantics for finite primitive numbers. |
isFinite(x) (global) | Coerces operands—strings may become finite numbers unexpectedly. |
_.isNumber(x) | Broader—still true for NaN/Infinity; pair with _.isFinite when needed. |
Pitfalls to avoid
Query strings arrive as text
Parse with Number/parseFloat first—lodash will not treat "42" as numeric.
Numbers survive; strings do not
Serialized payloads often mix strings—normalize schema fields before validation.
BigInt is not typeof number
typeof 1n === "bigint", so _.isFinite returns false—handle BigInt separately.
❓ FAQ
Summary
- Purpose: validate finite primitive numbers without accidental coercion.
- Reject: strings, boxed numbers,
Infinity, andNaN. - Next: explore more on Lodash _.isFunction().
_.isFinite returns true only when typeof value === "number" and the built-in isFinite test passes—so numeric strings, boxed numbers, null, and booleans are all false, unlike global isFinite("3").
6 people found this page helpful
