Produce a random sample from a list. Pass a number to return n random elements; otherwise a single random item is returned (official Underscore docs). Stable Collection helper — not deprecated.
Pass the required arguments shown below. Underscore collection helpers usually return a new value — check whether the source is mutated.
Syntax
Call _.sample. Start with this line.
Underscore
_.sample(list, [n])
Call
What it does
_.sample(list, [n])
Primary signature.
_.sample(...)
Call with the arguments your case needs.
Load Underscore with import _ from "underscore" (or the UMD CDN build) so these helpers are available as _.
Library declarations
Underscore
_.sample(list, [n])
Parameters
Parameter
Description
list
Required. Array, array-like, or plain object
n
Optional. How many unique elements to return
Return Value
Return Value
Description
*
One random element
*
New array of n unique elements
Underscore.js Examples
Example 1 — One Random Element
Omit n and Underscore returns a single item — not an array.
Underscore
_.sample([1, 2, 3, 4, 5, 6]);
// → 4 (example — any of 1…6)
With n._.sample(prizes, 2) yields an array of two values.
Same rules. Unique picks, source unchanged.
Compatibility
Browser & Runtime Support
_.sample ships with Underscore.js (1.x). It runs anywhere Underscore runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install underscore.
✓ Stable · Underscore 1.x
_.sample()
Available in [email protected] (UMD / ESM). Works in evergreen browsers and current Node LTS when Underscore is loaded. Uses Math.random under the hood.
100%With Underscore 1.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
_.sampleUnderscore 1.x+
Bottom line: Safe for production once Underscore is on the page or in your bundle. Not a native browser API — load Underscore first. Prefer a pinned CDN version (e.g. [email protected]). Native alternative: pick list[Math.floor(Math.random()*list.length)], or shuffle then slice for n items.
Frequently Asked Questions
_.sample(list, [n]) produces a random sample from a collection. Without n it returns one random element; with n it returns an array of n unique random elements.
No. Picks are without replacement. If n is larger than the list length, you get all elements in random order.
_.shuffle returns the whole list randomized. _.sample returns only one element (or n elements).
Yes. It samples the object's values. Keys are not returned.
No. It returns a new value or array; the source is unchanged.
Without n, you get undefined. With n, you get an empty array.