Lodash _.shuffle() method

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

What you’ll learn

  • How _.shuffle(collection) returns a new array containing every member in random order.
  • Why source arrays and strings stay unchanged while you iterate the shuffled copy.
  • When smaller helpers (sample, sampleSize) beat full permutations.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Read _.sampleSize() for partial random draws—shuffle always consumes the whole finite pool.

  • You accept pseudo-random ordering for UX—not security primitives.
  • You can open Try-it labs or run snippets locally.

Overview

_.shuffle is the kitchen-sink random ordering helper—flashcard decks, playlist reshuffles, randomized audit queues—without rewriting Fisher–Yates yourself.

Full permutation

Every element appears exactly once—multiplicity mirrors the source collection.

Immutable inputs

Original arrays/objects/strings remain stable—only the returned array is reordered.

Collections beyond arrays

Shuffle enumerable object values or characters pulled from strings.

Syntax

javascript
_.shuffle(collection)
  • collection: array, array-like string, or plain object Lodash can iterate.
  • Returns: new array whose elements are a random permutation of the collection’s members.
  • Randomness: pseudorandom—swap for crypto primitives when stakes are high.
1

Shuffle a numeric array

The multiset stays intact—only ordering changes—and the source array keeps its original sequence.

javascript
import shuffle from "lodash/shuffle";

const deck = [1, 2, 3, 4];
shuffle(deck);
// → permutation of [1,2,3,4]; deck still [1,2,3,4]
Try it Yourself
2

Shuffle values from a plain object

Keys determine enumeration order inputs only—the output is always an array of shuffled values.

javascript
import shuffle from "lodash/shuffle";

shuffle({ alpha: "x", beta: "y", gamma: "z" });
// → permutation of ["x","y","z"]
Try it Yourself
3

Shuffle characters in a string

Strings degrade into character tiles—useful for toy anagram generators alongside Unicode caveats for surrogate pairs.

javascript
import shuffle from "lodash/shuffle";

shuffle("abc");
// → array of "a", "b", "c" in random order
Try it Yourself

📋 _.shuffle vs sample, sampleSize, manual sorts

APIOutcomeBest when
_.shuffle(collection)Full-length permutation arrayYou need every element eventually, just randomized
_.sample(collection)Single elementOnly one winner matters
_.sampleSize(collection, n)n draws without exhausting the pool twicePolls or spot checks
array.sort(() => Math.random() - 0.5)Biased orderingAvoid—uniformity bugs; prefer shuffle helpers

Pitfalls to avoid

Crypto

Predictable decks

Card games for fun are fine; keyed tokens or lottery draws need audited RNGs.

Tests

Flaky specs

Stub Math.random in focused tests or wrap shuffle behind your own function—Lodash exposes no seed knob.

Unicode

Grapheme surprises

Emoji and combining marks may span multiple UTF-16 units—shuffle sees code units, not human glyphs.

❓ FAQ

No—it clones enumerable elements into a fresh array, shuffles that copy, and leaves the source untouched.
An array of the object’s values in random order—keys are not preserved in the result shape.
Lodash treats strings as sequences—shuffle returns an array of single-character strings.
Lodash aims for uniform random permutations via Fisher–Yates-style logic—still pseudo-random, not cryptographic.
You receive an empty array—nothing to permute.

Summary

Did you know?

_.shuffle returns a new array—your original array, object, or string literal is never reordered in place, making it safe beside immutable UI state patterns.

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