Lodash _.sample() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Collection

Definition

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

ParameterDescription
collectionRequired. Array or object to sample from

Return Value

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

How It Works

  1. Source. Four numbers at indexes 0 … 3.
  2. Pick index. Lodash chooses a random integer in that range.
  3. Return value. The element at that index (docs showed 2).
  4. Unchanged. The original array is not modified.

Example 2 — Sample from Strings

Useful for demos, quizzes, and “pick a winner” UIs.

Lodash
_.sample(["Ada", "Lin", "Sam", "Joy"]);
// → "Lin"   (example — any name is possible)
Try It Yourself

How It Works

  1. List. Four string elements.
  2. Draw. One index is chosen at random.
  3. Result. That string is returned as a single value (not an array).
  4. Repeat. Call _.sample again for an independent draw.

Example 3 — Sample Object Values

Plain objects are sampled by value, not by key.

Lodash
_.sample({ a: 10, b: 20, c: 30 });
// → 20   (example — 10, 20, or 30)
Try It Yourself

How It Works

  1. Values. Lodash builds [10, 20, 30] from the object.
  2. Sample. Picks one entry from that value list.
  3. Not the key. You get 20, not "b".
  4. Need a key? Sample _.keys(obj) instead, then look up the value.

Example 4 — Empty Collection

Sampling nothing yields undefined — no throw.

Lodash
_.sample([]);
// → undefined

_.sample({});
// → undefined
Try It Yourself

How It Works

  1. No items. There is no valid index to choose.
  2. Result. Lodash returns undefined.
  3. Safe. It does not throw — check before using the value.
  4. Guard. if (list.length) { … _.sample(list) }.

Example 5 — Single-Element Collection

With only one choice, every draw returns that element.

Lodash
_.sample(["only"]);
// → "only"
Try It Yourself

How It Works

  1. Length 1. The only valid index is 0.
  2. Always. Random choice collapses to that single element.
  3. Still safe. No mutation; returns the value as usual.
  4. Tip. Useful as a default path when a filtered list may shrink to one item.

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

Next: _.sampleSize()

Pick multiple unique random elements in one call.

_.sampleSize() tutorial →

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