Lodash _.sample() method

Beginner
⏱️ 6 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

What you’ll learn

  • How _.sample(collection) returns one pseudo-random member—arrays, plain-object values, or string characters.
  • Why empty collections yield undefined, and how that differs from singleton picks.
  • When to graduate to sampleSize, shuffle, or native crypto APIs.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Continue with _.sampleSize() for multi-draw batches or _.shuffle() for full permutations.

  • You understand that helper randomness is not automatically crypto-grade.
  • You can open Try-it labs or run snippets locally.

Overview

_.sample answers “give me any element”—feature flags, dice rolls, placeholder users—without building intermediate arrays.

Single draw

One call returns exactly one element—or undefined when nothing exists to choose.

Objects & strings

Plain objects contribute values; strings contribute characters—still one return value.

Non-destructive

Sampling reads only—your source collection stays unchanged.

Syntax

javascript
_.sample(collection)
  • collection: array, array-like string, or plain object Lodash can iterate.
  • Returns: one arbitrary element/value/character, or undefined when empty.
  • Randomness: pseudo-random via Lodash internals—fine for UX flourishes, not secret generation.
1

Pick one element from an array

Each invocation may return any member—rerunning the Try-it lab proves the variability.

javascript
import sample from "lodash/sample";

sample([1, 2, 3, 4]);
// → any of 1, 2, 3, or 4
Try it Yourself
2

Sample a value from a plain object

Enumeration considers values only—you receive 1, 2, or 3, never the key labels.

javascript
import sample from "lodash/sample";

sample({ alpha: 1, beta: 2, gamma: 3 });
// → 1, 2, or 3
Try it Yourself
3

Sample a character from a string

Strings act like character sequences—surrogate pairs can surface as separate code units depending on engine inputs.

javascript
import sample from "lodash/sample";

sample("hello");
// → one of "h", "e", "l", "l", "o"
Try it Yourself

📋 _.sample vs sampleSize, shuffle, manual indexing

APIOutcomeBest when
_.sample(collection)Single random memberYou only need one pick
_.sampleSize(collection, n)Up to n distinct picksWeighted giveaways, bingo draws—multiple winners without reshuffling entirely
_.shuffle(collection)New array in random orderYou consume every element in shuffled sequence
arr[Math.floor(Math.random() * arr.length)]Random array slotNo Lodash dependency on dense arrays

Pitfalls to avoid

Crypto

Security-sensitive picks

Session IDs, coupons, or prizes facing attackers need crypto RNGs—not Lodash convenience helpers.

Tests

Flaky assertions

Stub randomness or inject deterministic factories—Lodash does not ship seeded sampling.

Empty

Silent undefined

Guard empty collections before dereferencing the sampled value.

❓ FAQ

No—it only reads and returns one chosen element (or character); inputs stay untouched.
Lodash returns undefined—guard empty collections before sampling.
It returns a randomly chosen value among enumerable values—not the key.
No—use Web Crypto or Node crypto modules when unpredictability must resist attackers.
Lodash treats strings like sequences—sample returns a single-character string.

Summary

Did you know?

_.sample uses Lodash’s internal pseudo-random source—not crypto.getRandomValues. For security-sensitive picks (tokens, lottery draws), build an explicit RNG pipeline instead of relying on convenience helpers.

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.

6 people found this page helpful