Lodash isFinite vs Native Global isFinite
This article explores how Lodash’s _.isFinite method
handles values—specifically numeric strings—compared to JavaScript's
native global isFinite function. While JavaScript's global
function applies aggressive type coercion that can produce unexpected
false positives, Lodash's implementation enforces strict type checking.
Understanding this distinction is essential for preventing data
validation bugs when parsing numeric inputs in JavaScript
applications.
The Problem with Native
Global isFinite
JavaScript's built-in global isFinite() function
converts its argument to a number before evaluating whether it is
finite. Because of this implicit type coercion, passing a numeric string
causes the engine to parse the string as a number:
isFinite("42"); // true
isFinite("3.14"); // trueWhile this may seem convenient, the global method's type coercion extends to non-numeric types and empty values, leading to notorious JavaScript edge cases:
isFinite(""); // true (empty string coerces to 0)
isFinite(" "); // true (whitespace coerces to 0)
isFinite(null); // true (null coerces to 0)
isFinite([]); // true (empty array coerces to 0)In these cases, non-numeric values and empty inputs are reported as finite numbers, which can easily introduce logic errors into validation routines.
How Lodash
_.isFinite Evaluates Values
Lodash’s _.isFinite method evaluates values safely by
verifying that the input is actually a number primitive or object before
checking its finiteness. Under the hood, Lodash ensures that the value's
type is strictly numeric:
_.isFinite(42); // true
_.isFinite(3.14); // true
_.isFinite(Infinity); // false
_.isFinite(NaN); // falseWhen evaluated against numeric strings or coercive edge cases,
_.isFinite does not coerce the value to a number. It
immediately returns false because the input is not a number
primitive:
_.isFinite("42"); // false
_.isFinite(""); // false
_.isFinite(null); // false
_.isFinite([]); // falseKey Differences Summary
- Type Coercion: Global
isFinitecoerces all arguments via theNumber()conversion rules before checking finiteness. Lodash’s_.isFinitechecks the type first and rejects non-numeric types outright. - Numeric String Handling: Global
isFinite("123")evaluates totrue. Lodash_.isFinite("123")evaluates tofalse. - Falsy and Empty Values: Global
isFiniteincorrectly evaluates""," ", andnullas finite numbers (0). Lodash safely returnsfalsefor all of them.
Relation to
Number.isFinite
Lodash’s behavior mirrors the modern standard method
Number.isFinite(), introduced in ECMAScript 2015 (ES6).
Both Number.isFinite() and Lodash’s
_.isFinite() avoid the legacy coercion pitfalls of the
global isFinite function.
If an application requires treating valid numeric strings as numbers,
developers must explicitly parse them using Number(),
parseFloat(), or parseInt() before passing the
result to _.isFinite. This guarantees that string
conversion is intentional rather than an unintended side effect of
validation.