Lodash _.lte() method
What you’ll learn
- How
_.lte(value, other)mirrors_.ltbut includes equality. - Why string-vs-string skips number coercion (lexicographic
<=). - How dates compare cleanly thanks to
Number(date)→ timestamp. - Common range-check and inventory patterns (with the fixed direction).
Prerequisites
You finished the _.lt tutorial; the rules below are the same, only the equal-to branch flips.
- You understand JavaScript’s
<=operator and basic coercion. - Try-it labs load lodash from the CDN.
Overview
Lodash builds _.lte through the same relational-operation helper as _.lt: string pair → lexicographic <=, otherwise both arguments are coerced via Number(...). The only behavioral difference is that equal values return true.
Includes equality
_.lte(3, 3) → true; the “edge inclusive” flavor of _.lt.
Date-friendly
Number(date) = timestamp, so deadlines and timestamps compare correctly.
NaN-aware
undefined → NaN → false. Even _.lte(NaN, NaN) is false.
Syntax
_.lte(value, other) - value: the first value to compare.
- other: the second value to compare against.
- Returns:
trueifvalueis less than or equal toother; otherwisefalse.
Number comparison basics
The lodash docs trio — note that the middle case flips from _.lt.
import lte from "lodash/lte";
console.log(
"1 <= 3: " + lte(1, 3) + "\n" + // true (lodash docs)
"3 <= 3: " + lte(3, 3) + "\n" + // true (lodash docs — differs from _.lt)
"3 <= 1: " + lte(3, 1) // false (lodash docs)
); Inventory check (edge-inclusive)
Use _.lte to fulfil an order when the request fits within stock—equal counts still pass.
import lte from "lodash/lte";
const inStock = 100;
function canFulfill(requested) {
return lte(requested, inStock);
}
console.log(
"request 60: " + canFulfill(60) + "\n" + // true
"request 100: " + canFulfill(100) + "\n" + // true (edge inclusive)
"request 120: " + canFulfill(120) // false
); Strings, dates, and nullish
Same coercion rules as _.lt: two strings stay strings, dates become timestamps, null becomes 0, undefined becomes NaN.
import lte from "lodash/lte";
const today = new Date("2026-05-12");
const deadline = new Date("2026-05-12");
console.log(
"'apple' <= 'apple': " + lte("apple", "apple") + "\n" + // true
"'10' <= '9': " + lte("10", "9") + "\n" + // true (lexicographic)
"today <= deadline: " + lte(today, deadline) + "\n" + // true (same timestamp)
"null <= 0: " + lte(null, 0) + "\n" + // true (Number(null) = 0)
"undefined <= 1: " + lte(undefined, 1) // false (NaN)
); 📋 _.lte vs related operators
| API / pattern | Behavior |
|---|---|
_.lte(a, b) | Includes equality (<=); string pair → lexicographic, otherwise number coercion. |
_.lt(a, b) | Strict less-than; equal values return false. |
_.gte(a, b) | Mirror of _.lte—greater than or equal. |
a <= b | Native operator; equivalent for primitives but never branches on string-pair detection. |
Pitfalls to avoid
Mind the argument order
A common bug: writing _.lte(available, requested) when you meant _.lte(requested, available). Re-read the call as “A is <= B” before shipping.
Digit strings sort lexicographically
_.lte('10', '9') is true. For numeric order, convert with Number(...) first.
BigInt throws
Lodash routes through Number(...), which can’t convert BigInt. Use the native <= operator for arbitrary-precision integers.
❓ FAQ
Summary
- Purpose: return
truewhenvalue <= otherwith predictable coercion. - Remember: only the equality branch differs from
_.lt—great for inclusive ranges, deadlines, capacity limits. - Next: see the Lodash _.toArray() index for
_.toArrayand friends.
_.lte is the only relational helper in lodash where a === b returns true—_.lt, _.gt, and the strict </> operators all return false for equal values. That single bit of difference is why the inventory or deadline pattern (“include the edge”) reaches for _.lte.
5 people found this page helpful
