Underscore.js _.sample() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Collection

Definition

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])
CallWhat 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

ParameterDescription
listRequired. Array, array-like, or plain object
nOptional. How many unique elements to return

Return Value

Return ValueDescription
*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)
Try It Yourself

How It Works

  1. No second arg. Underscore picks exactly one element.
  2. Bare value. Result is a number here, not [4].
  3. Fair pick. Each index has a chance to win.
  4. Source safe. The list is left unchanged.

Example 2 — n Unique Elements

Pass n to get an array of distinct random picks.

Underscore
_.sample([1, 2, 3, 4, 5, 6], 3);
// → [1, 6, 2]   (example — 3 unique values)
Try It Yourself

How It Works

  1. Ask for 3. Result shape is always an array.
  2. No repeats. Each pick is unique within this call.
  3. Order random. Positions in the result are shuffled.
  4. Use case. Quiz questions, raffle winners, random subsets.

Example 3 — n Larger Than Length

Asking for more items than exist returns the whole list shuffled.

Underscore
_.sample(["red", "green", "blue"], 10);
// → ["blue", "red", "green"]  (length 3, random order)
Try It Yourself

How It Works

  1. Cap at length. You cannot invent extra items.
  2. All included. Result length equals the source length.
  3. Still random. Order is shuffled like a small shuffle.
  4. No error. Oversized n is safe.

Example 4 — Sample Object Values

On a plain object, Underscore picks among the values.

Underscore
var prizes = { gold: "Car", silver: "Bike", bronze: "Mug" };

_.sample(prizes);
// → "Bike"   (example — one of the values)
Try It Yourself

How It Works

  1. Values only. Keys like gold are not returned.
  2. One pick. Without n, you get a single value.
  3. With n. _.sample(prizes, 2) yields an array of two values.
  4. Same rules. Unique picks, source unchanged.

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 Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.sample Underscore 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.

Next: _.toArray()

Turn any collection into a real JavaScript array.

_.toArray() →

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.

10 people found this page helpful