Lodash _.toFinite() method

Beginner
⏱️ 6 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

What you’ll learn

  • How _.toFinite(value) guarantees a real number (no NaN, no ±Infinity).
  • Why falsy inputs collapse to 0 and what happens to -0.
  • The hex / binary / octal / scientific string forms it can parse.
  • Where _.toFinite beats Number()—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

javascript
_.toFinite(value)
  • value: the value to convert.
  • Returns: a finite number. Never NaN and never ±Infinity.
1

Numbers, strings, & the infinity clamp

The four official lodash docs cases—straight numbers, scientific limits, infinity, and a numeric string.

javascript
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
);
Try it Yourself
2

Falsy & non-numeric inputs → 0

This is the main reason to reach for _.toFinite over Number()—you never get NaN back.

javascript
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
);
Try it Yourself
3

Hex, binary, octal, whitespace

Internally _.toFinite calls _.toNumber, which trims whitespace and accepts the three numeric literal prefixes.

javascript
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)
);
Try it Yourself

📋 _.toFinite vs related conversions

Input_.toFinite_.toNumberNumber()
'abc'0NaNNaN
Infinity1.7976e+308InfinityInfinity
NaN0NaNNaN
null000
'0x1f'313131

Pitfalls to avoid

Defaults

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.

Arrays

Single-element arrays coerce

_.toFinite([5]) is 5; _.toFinite([5, 6]) is 0. Same trap as the native +arr coercion.

BigInt

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

Number('abc') is NaN; _.toFinite('abc') is 0. Number(Infinity) is Infinity; _.toFinite(Infinity) is Number.MAX_VALUE. Both parse '3.2' the same way.
Yes—internally it routes through _.toNumber, which trims leading/trailing whitespace before parsing, so _.toFinite(' 7 ') returns 7.
Yes. _.toFinite('0x1f') is 31, _.toFinite('0b11') is 3, _.toFinite('0o17') is 15. Signed bad-hex like '-0x10' is treated as invalid and returns 0.
It throws. _.toFinite(1n) raises 'Cannot convert a BigInt value to a number'. Convert with Number(bigint) at your own risk first, or compare with native operators.

Summary

  • Purpose: guarantee a finite, math-safe number from anything except BigInt.
  • Remember: NaN, falsy values, and unparseable strings all collapse to 0; ±Infinity clamps to ±Number.MAX_VALUE.
  • Next: continue with Lodash _.toInteger()_.toInteger, _.toLength, and friends.
Did you know?

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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful