Lodash _.gt() method
What you’ll learn
- How
_.gt(value, other)maps to JavaScript’s>comparison. - Ordering for numbers, strings, and
Datevalues. - Why
NaNand many object pairs yieldfalse. - When to pair
_.gtwith_.gte,_.lt, and_.lte.
Prerequisites
Comfort with the > operator and a rough sense of when JavaScript coerces values during comparisons.
- You can read numeric and string ordering examples.
- You can open Try-it labs in the browser.
Overview
_.gt is a small, readable wrapper around strict greater-than tests. Use it in predicates and comparators when you want lodash-style imports and a name that reads naturally in English.
Numeric order
Compare magnitudes the same way > does after standard numeric conversion rules.
String ordering
Lexicographic by UTF-16 code units—fine for ASCII tokens, not for localized words.
NaN guards
Any comparison with NaN is false; validate inputs when data might be invalid.
Syntax
_.gt(value, other) - value: left-hand side of the comparison.
- other: right-hand side of the comparison.
- Returns:
trueifvalue > otherunder JavaScript rules; otherwisefalse.
Numbers and strings
For finite numbers, ordering matches ordinary arithmetic. For strings, comparison is lexicographic.
import gt from "lodash/gt";
gt(5, 3); // true
gt(3, 5); // false
gt("b", "a"); // true Dates and decimals
Date objects are compared via their time values. Decimals follow numeric ordering.
import gt from "lodash/gt";
var later = new Date("2020-01-01");
var earlier = new Date("2019-12-31");
gt(later, earlier); // true
gt(3.14, 3); // true Mixed types and coercion
Relational comparisons can coerce operands (for example string to number). Stay explicit in production code when types vary.
import gt from "lodash/gt";
gt(10, "9"); // true (numeric comparison path)
gt("10", 9); // true 📋 _.gt vs _.gte vs _.lt
| Method | Meaning |
|---|---|
_.gt(a, b) | a > b |
_.gte(a, b) | a >= b |
_.lt(a, b) | a < b |
_.lte(a, b) | a <= b |
Pitfalls to avoid
Invalid numbers
_.gt(NaN, x) and _.gt(x, NaN) are always false.
Surprising object pairs
Plain objects convert via ToPrimitive; two {} values often both become [object Object], so _.gt({}, {}) is false.
Localized text
String ordering is not linguistically correct for user-visible sorting; use collation APIs when needed.
❓ FAQ
Summary
- Purpose: strict greater-than in one named call.
- Semantics: identical to the binary
>operator for the same operands. - Next: explore more on Lodash _.gte().
_.gt delegates to the same relational comparison as the binary > operator: the result is true only when value > other after JavaScript's comparison rules (including coercion where the language allows it).
6 people found this page helpful
