Lodash Number Methods

Index
About This Index
This page is the Lodash Number index for the tutorial track. Each row links to a full guide with syntax, examples, and FAQs.
Learn math aggregators on /lodash/math. Use this page for bounds and randomness with _.clamp(), _.inRange(), and _.random().
Beginner path.
Start with _.clamp(), then _.inRange() for half-open intervals, and _.random() for bounded picks (not crypto).
Syntax
How to Call a Number Method
Pass a number and bounds to the Lodash helper:
Lodash
import _ from "lodash";
_.clamp(-10, -5, 5); // -5
_.clamp(10, -5, 5); // 5
_.clamp(3, -5, 5); // 3
_.inRange(3, 2, 4); // true (2 <= 3 < 4)
_.inRange(4, 2, 4); // false (end exclusive)
_.inRange(2, 2); // false (defaults start=0, end=2)
_.random(0, 5); // integer 0..5
_.random(1.2, 5.2); // float in range
_.random(5); // integer 0..5 Browse
Lodash Number Methods Table
All number method guides in this Lodash tutorial series (3 tutorials).
Bounds
2 methodsClamp values and test half-open numeric ranges.
| Method | Description | Tutorial |
|---|---|---|
_.clamp() | Constrain a number between a lower and upper bound. | Try it |
_.inRange() | Return true when the number lies in the half-open interval [start, end). | Try it |
Generate
1 methodCreate bounded random integers or floats.
| Method | Description | Tutorial |
|---|---|---|
_.random() | Produce a random integer or floating value between bounds. | Try it |
Frequently Asked Questions
Three helpers: _.clamp pins a value inside bounds, _.inRange tests membership on a half-open numeric interval, and _.random generates bounded integers or floats.
It uses a half-open interval [start, end), can default start to 0, and swaps bounds when start exceeds end—so you do not accidentally include the exclusive end.
No. It uses Math.random under the hood, which is not cryptographically secure. Use crypto.getRandomValues or crypto.randomBytes for tokens and keys.
Prefer per-method imports: import clamp from 'lodash/clamp' or import { clamp } from 'lodash-es'. You can also use const _ = require('lodash') and call _.clamp().
Start with _.clamp(), then _.inRange() and _.random() from the table below.
Start with _.clamp()
Open the clamp guide—syntax, examples, and FAQs.
8 people found this page helpful
