Lodash _.sample() method
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
_.sample(collection) - collection: array, array-like string, or plain object Lodash can iterate.
- Returns: one arbitrary element/value/character, or
undefinedwhen empty. - Randomness: pseudo-random via Lodash internals—fine for UX flourishes, not secret generation.
Pick one element from an array
Each invocation may return any member—rerunning the Try-it lab proves the variability.
import sample from "lodash/sample";
sample([1, 2, 3, 4]);
// → any of 1, 2, 3, or 4 Sample a value from a plain object
Enumeration considers values only—you receive 1, 2, or 3, never the key labels.
import sample from "lodash/sample";
sample({ alpha: 1, beta: 2, gamma: 3 });
// → 1, 2, or 3 Sample a character from a string
Strings act like character sequences—surrogate pairs can surface as separate code units depending on engine inputs.
import sample from "lodash/sample";
sample("hello");
// → one of "h", "e", "l", "l", "o" 📋 _.sample vs sampleSize, shuffle, manual indexing
| API | Outcome | Best when |
|---|---|---|
_.sample(collection) | Single random member | You only need one pick |
_.sampleSize(collection, n) | Up to n distinct picks | Weighted giveaways, bingo draws—multiple winners without reshuffling entirely |
_.shuffle(collection) | New array in random order | You consume every element in shuffled sequence |
arr[Math.floor(Math.random() * arr.length)] | Random array slot | No Lodash dependency on dense arrays |
Pitfalls to avoid
Security-sensitive picks
Session IDs, coupons, or prizes facing attackers need crypto RNGs—not Lodash convenience helpers.
Flaky assertions
Stub randomness or inject deterministic factories—Lodash does not ship seeded sampling.
Silent undefined
Guard empty collections before dereferencing the sampled value.
❓ FAQ
Summary
- Purpose:
_.sample(collection)returns one pseudo-random element without mutating inputs. - Contrast: reach for
sampleSizeorshufflewhen you need multiple draws or full permutations. - Next: Lodash _.sampleSize(), Lodash _.reject() (previous), or collection hub.
_.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.
6 people found this page helpful
