Lodash _.inRange() method
What you’ll learn
- The half-open rule:
start <= number < end(upper bound is not included). - The two-argument form
_.inRange(number, end)and howstartdefaults to0. - How lodash swaps reversed
start/endbefore comparing—unlike _.clamp, which does not reorder bounds. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
You should be comfortable with numeric comparisons and why slice(start, end) excludes end. Read _.clamp() first if inclusive bounds are fresher in your head than half-open ones.
- Booleans: this API returns
trueorfalse, never the number you passed in. - Finite values: inputs are coerced toward finite numbers;
NaNcomparisons yieldfalse.
Overview
_.inRange answers “does this number sit inside the window?” using a half-open interval. That matches how many APIs express lengths and slices, so you can reuse the same mental model for indexes, timestamps, and normalized scores.
Half-open interval
Inclusive low, exclusive high—end itself returns false.
Optional start
Omit the middle argument to test against [0, end).
Boolean guard
Ideal for validation and early returns without mutating the value under test.
Syntax
_.inRange(number, [start=0], end) - number: value to test; coerced with
toFinite-style handling in the implementation. - start / end: bounds of the interval; if only two numbers are passed, the second is
endandstartdefaults to0. - Returns:
truewhenstart <= number < endafter orderingstartandend; otherwisefalse.
Half-open membership
Values on the lower edge pass; values on the upper edge fail because end is excluded.
import inRange from "lodash/inRange";
inRange(3, 2, 4);
// => true (2 <= 3 < 4)
inRange(2, 2, 4);
// => true (start is inclusive)
inRange(4, 2, 4);
// => false (end is exclusive) Two-argument form (start defaults to 0)
When you only pass number and end, lodash assumes start === 0—perfect for “is this a valid non-negative index below length?” style checks.
import inRange from "lodash/inRange";
inRange(2, 5);
// => true (0 <= 2 < 5)
inRange(5, 5);
// => false (0 <= 5 < 5 is false) Reversed start and end
Arguments that arrive backwards are swapped before the half-open test—so you still get a sensible interval.
import inRange from "lodash/inRange";
// Looks like start > end, but lodash orders to [2, 4)
inRange(3, 4, 2);
// => true 📋 _.inRange vs _.clamp
| Topic | _.inRange | _.clamp |
|---|---|---|
| Returns | true / false | A number inside inclusive bounds |
| Upper bound | Exclusive (< end) | Inclusive (<= upper) |
| Typical use | Validation, index windows | Sliders, physics integration |
Pick inRange when you only need a yes/no gate; pick clamp when you need the corrected value back.
Pitfalls to avoid
Treating end as inclusive
UI copy often says “1 through 10” inclusively; _.inRange(n, 1, 11) matches that, while _.inRange(n, 1, 10) excludes 10.
NaNSilent false
Invalid numbers do not throw—they fail the range test. Pair with explicit Number.isFinite when debugging user input.
Two vs three arguments
Accidentally calling inRange(start, end, number) permutes meaning; keep the lodash order (number, start, end).
❓ FAQ
Summary
- Purpose: boolean half-open range test with optional default start at
0. - Remember:
endis exclusive; reversed bounds swap automatically. - Next: Lodash _.random(), the Number hub, or the official Lodash docs for _.inRange.
_.inRange tests start <= number < end after coercing with toFinite and swapping start/end when the first is greater—the upper bound is exclusive, matching slice-style ranges.
6 people found this page helpful
