Gets a random element from a collection. For arrays it picks an index; for plain objects it samples from the values. Part of Lodash 4.x Collection (since 2.0.0).
Pass the required arguments shown below. Lodash Collection helpers usually return a new value — check whether the source is mutated.
Syntax
Call _.sample on a collection. Start with this line.
Lodash
_.sample(collection)
Call
What it does
_.sample(collection)
Primary signature.
_.sample(collection)
Call with a collection argument.
Prefer import sample from "lodash/sample" when you want a smaller bundle.
Library declarations
Lodash
_.sample(collection)
Parameters
Parameter
Description
collection
Required. Array or object to sample from
Return Value
Return Value
Description
*
One random element, or undefined if empty
Lodash Examples
Example 1 — Official: Sample an Array
Pick one number from four — any index is equally likely.
Lodash
_.sample([1, 2, 3, 4]);
// → 2 (example from the docs; your result may differ)
Always. Random choice collapses to that single element.
Still safe. No mutation; returns the value as usual.
Tip. Useful as a default path when a filtered list may shrink to one item.
Compatibility
Browser & Runtime Support
_.sample is a Lodash 4.x Collection helper (since 2.0.0). It runs anywhere Lodash runs — modern browsers (via CDN or bundle) and Node.js / Bun / Deno with npm install lodash.
✓ Stable · Lodash 2.0.0+
_.sample()
Available in [email protected] full builds and the lodash/sample modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.
100%With Lodash 4.x
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
_.sampleLodash 4.x+
Bottom line: Safe for production once Lodash is on the page or in your bundle. Prefer import sample from "lodash/sample" for smaller builds. Not a native browser API — load Lodash first.
Frequently Asked Questions
_.sample(collection) returns one element chosen at random from an array, or from an object's values. It does not mutate the collection.
_.sample([]) and _.sample({}) return undefined.
sample returns a single value. _.sampleSize(collection, n) returns an array of up to n unique random elements.
No. It uses Lodash's pseudo-random helper, not crypto.getRandomValues. Do not use it for tokens, lotteries, or security-sensitive picks.
Yes. Lodash samples from the object's values (via _.values), not from the keys.
Use import sample from "lodash/sample" or require("lodash/sample") for tree-shaking. Full build: import _ from "lodash" then _.sample.