Lodash _.toFinite() method
What you’ll learn
- How
_.toFinite(value)guarantees a real number (noNaN, no±Infinity). - Why falsy inputs collapse to
0and what happens to-0. - The hex / binary / octal / scientific string forms it can parse.
- Where
_.toFinitebeatsNumber()—and the BigInt pitfall it doesn’t solve.
Prerequisites
You’ve used Number() / parseFloat() and know what NaN and Infinity are.
- You’ve seen IEEE-754 boundaries (
Number.MAX_VALUE,Number.MIN_VALUE). - Try-it labs load lodash from the CDN.
Overview
The algorithm in three steps: falsy → 0 (preserving -0); else _.toNumber(value); then clamp ±Infinity to ±Number.MAX_VALUE and turn NaN into 0.
Always a real number
No NaN, no Infinity—safe to feed into math.
String-savvy
Trims whitespace, parses decimals, hex (0x), binary (0b), octal (0o).
Bounded output
±Infinity clamps to ±Number.MAX_VALUE rather than overflowing.
Syntax
_.toFinite(value) - value: the value to convert.
- Returns: a finite
number. NeverNaNand never±Infinity.
Numbers, strings, & the infinity clamp
The four official lodash docs cases—straight numbers, scientific limits, infinity, and a numeric string.
import toFinite from "lodash/toFinite";
console.log(
"3.2: " + toFinite(3.2) + "\n" + // 3.2
"MIN_VALUE: " + toFinite(Number.MIN_VALUE) + "\n" + // 5e-324
"Infinity: " + toFinite(Infinity) + "\n" + // 1.7976931348623157e+308 (Number.MAX_VALUE)
"'3.2' string: " + toFinite("3.2") // 3.2
); Falsy & non-numeric inputs → 0
This is the main reason to reach for _.toFinite over Number()—you never get NaN back.
import toFinite from "lodash/toFinite";
console.log(
"null: " + toFinite(null) + "\n" + // 0
"undefined: " + toFinite(undefined) + "\n" + // 0
"NaN: " + toFinite(NaN) + "\n" + // 0 (vs Number(NaN) === NaN)
"'abc': " + toFinite("abc") + "\n" + // 0 (vs Number('abc') === NaN)
"false: " + toFinite(false) + "\n" + // 0
"true: " + toFinite(true) // 1
); Hex, binary, octal, whitespace
Internally _.toFinite calls _.toNumber, which trims whitespace and accepts the three numeric literal prefixes.
import toFinite from "lodash/toFinite";
console.log(
"'0x1f' hex: " + toFinite("0x1f") + "\n" + // 31
"'0b11' binary: " + toFinite("0b11") + "\n" + // 3
"'0o17' octal: " + toFinite("0o17") + "\n" + // 15
"' 7 ' trim: " + toFinite(" 7 ") + "\n" + // 7
"'1e309' clamp: " + toFinite("1e309") // 1.7976931348623157e+308 (overflowed string → MAX_VALUE)
); 📋 _.toFinite vs related conversions
| Input | _.toFinite | _.toNumber | Number() |
|---|---|---|---|
'abc' | 0 | NaN | NaN |
Infinity | 1.7976e+308 | Infinity | Infinity |
NaN | 0 | NaN | NaN |
null | 0 | 0 | 0 |
'0x1f' | 31 | 31 | 31 |
Pitfalls to avoid
Bad input silently becomes 0
_.toFinite('abc') is 0—great for downstream math, terrible if you needed to detect invalid input. Validate first or pair with _.defaultTo.
Single-element arrays coerce
_.toFinite([5]) is 5; _.toFinite([5, 6]) is 0. Same trap as the native +arr coercion.
BigInt throws
_.toFinite(1n) raises “Cannot convert a BigInt value to a number.” Convert with Number(bigint) first if you must, accepting potential precision loss.
❓ FAQ
Summary
- Purpose: guarantee a finite, math-safe number from anything except BigInt.
- Remember:
NaN, falsy values, and unparseable strings all collapse to0;±Infinityclamps to±Number.MAX_VALUE. - Next: continue with Lodash _.toInteger() —
_.toInteger,_.toLength, and friends.
The ceiling _.toFinite(Infinity) hits is exactly Number.MAX_VALUE (1.7976931348623157e+308)—the largest finite IEEE-754 double. Lodash clamps both signs to ±Number.MAX_VALUE, so _.toFinite('1e309') (which overflows during parsing) also lands there.
6 people found this page helpful
