Lodash _.lt() method
What you’ll learn
- How
_.lt(value, other)resolves to a cleantrue/false. - Why string-vs-string skips number coercion while mixed pairs don’t.
- What happens with
null,undefined, andNaN. - When to prefer
_.ltover the raw<operator (and when not).
Prerequisites
You’re comfortable with JavaScript’s < operator and the basics of type coercion (Number(value)).
- You know that
NaN< anything is alwaysfalse. - Try-it labs load lodash from the CDN.
Overview
Lodash wires _.lt through a relational-operation helper: strings on both sides → lexicographic <, otherwise both arguments are coerced with Number(...) first. Strict-equal values return false.
Number-friendly
Mixed types coerce: _.lt('5', 10) → true.
String-aware
Two strings stay strings — lexicographic order, so '10' < '9' is true.
NaN-aware
undefined → NaN → comparison is always false.
Syntax
_.lt(value, other) - value: the first value to compare.
- other: the second value to compare against.
- Returns:
trueifvalueis less thanother; otherwisefalse.
Number comparison basics
The lodash docs trio: smaller wins, equal returns false, bigger returns false.
import lt from "lodash/lt";
console.log(
"1 < 3: " + lt(1, 3) + "\n" + // true (lodash docs)
"3 < 3: " + lt(3, 3) + "\n" + // false (lodash docs)
"3 < 1: " + lt(3, 1) // false (lodash docs)
); Strings stay lexicographic
When both args are strings, lodash skips number coercion—so digit strings compare character by character.
import lt from "lodash/lt";
console.log(
"apple < banana: " + lt("apple", "banana") + "\n" + // true (a < b)
"apple < Apple: " + lt("apple", "Apple") + "\n" + // false (uppercase has lower code point)
"'10' < '9': " + lt("10", "9") // true (lexicographic, '1' < '9')
); Coercion: nullish & mixed types
When the pair isn’t two strings, lodash calls Number(...) on each side. null becomes 0, undefined becomes NaN.
import lt from "lodash/lt";
console.log(
"'5' < 10: " + lt("5", 10) + "\n" + // true (Number('5') = 5)
"null < 1: " + lt(null, 1) + "\n" + // true (Number(null) = 0)
"undefined < 1: " + lt(undefined, 1) + "\n" + // false (NaN)
"NaN < 1: " + lt(NaN, 1) // false (NaN never compares)
); 📋 _.lt vs related operators
| API / pattern | Behavior |
|---|---|
_.lt(a, b) | String pair → lexicographic; otherwise both coerced to Number. |
a < b | Native operator—uses standard abstract relational comparison; behaviour is similar but never special-cases the string pair detection. |
_.lte(a, b) | Includes equality (<=). |
_.gt(a, b) | Mirror operation—greater than. |
Pitfalls to avoid
Digit strings sort lexicographically
_.lt('10', '9') is true. Convert numeric strings to numbers first if you need numeric order: _.lt(Number(a), Number(b)).
undefined always loses
Number(undefined) is NaN, and any NaN comparison returns false. Guard for missing data instead of relying on _.lt to be truthy.
BigInt throws
Lodash routes through Number(...), which can’t convert BigInts. Use the native < operator (1n < 2n) when working with arbitrary-precision integers.
❓ FAQ
Summary
- Purpose: return
truewhenvalue < otherwith predictable coercion. - Remember: two strings stay strings; otherwise both sides go through
Number(). - Next: continue with the Lodash _.lte() index, where
_.lteand_.gtlive.
Lodash special-cases string + string pairs—_.lt('10', '9') returns true because both stay as strings and JavaScript compares them lexicographically. Mix in a number and lodash coerces both with Number(...), so _.lt('10', 9) returns false.
5 people found this page helpful
