Lodash _.inRange() method

Beginner
⏱️ 6 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

What you’ll learn

  • The half-open rule: start <= number < end (upper bound is not included).
  • The two-argument form _.inRange(number, end) and how start defaults to 0.
  • How lodash swaps reversed start / end before 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 true or false, never the number you passed in.
  • Finite values: inputs are coerced toward finite numbers; NaN comparisons yield false.

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

javascript
_.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 end and start defaults to 0.
  • Returns: true when start <= number < end after ordering start and end; otherwise false.
1

Half-open membership

Values on the lower edge pass; values on the upper edge fail because end is excluded.

javascript
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)
Try it Yourself
2

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.

javascript
import inRange from "lodash/inRange";

inRange(2, 5);
// => true   (0 <= 2 < 5)

inRange(5, 5);
// => false  (0 <= 5 < 5 is false)
Try it Yourself
3

Reversed start and end

Arguments that arrive backwards are swapped before the half-open test—so you still get a sensible interval.

javascript
import inRange from "lodash/inRange";

// Looks like start > end, but lodash orders to [2, 4)
inRange(3, 4, 2);
// => true
Try it Yourself

📋 _.inRange vs _.clamp

Topic_.inRange_.clamp
Returnstrue / falseA number inside inclusive bounds
Upper boundExclusive (< end)Inclusive (<= upper)
Typical useValidation, index windowsSliders, physics integration

Pick inRange when you only need a yes/no gate; pick clamp when you need the corrected value back.

Pitfalls to avoid

Off-by-one

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.

NaN

Silent false

Invalid numbers do not throw—they fail the range test. Pair with explicit Number.isFinite when debugging user input.

Arity

Two vs three arguments

Accidentally calling inRange(start, end, number) permutes meaning; keep the lodash order (number, start, end).

❓ FAQ

Exclusive. _.inRange(n, start, end) requires start <= n < end (after any swap). The value end itself is outside the interval—just like array.slice(start, end).
Lodash treats the second number as end and defaults start to 0. So _.inRange(2, 5) checks 0 <= 2 < 5, which is true.
clamp returns a number pinned inside inclusive bounds. inRange returns a boolean membership test on a half-open interval [start, end).
Lodash swaps them before testing (documented in the source), so the interval is always treated as the ordered pair. _.clamp does not share this behavior—do not assume the two APIs normalize bounds the same way.
false. Non-finite numbers fail the comparison chain.
Use import inRange from "lodash/inRange"; for ESM or const inRange = require('lodash/inRange') in CommonJS.

Summary

Did you know?

_.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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful