Underscore.js _.shuffle() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Collection

Definition

Returns a shuffled copy of the list, using a version of the Fisher-Yates shuffle (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 _.shuffle. Start with this line.

Underscore
_.shuffle(list)
CallWhat it does
_.shuffle(list)Primary signature.
_.shuffle(...)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
_.shuffle(list)

Parameters

ParameterDescription
listRequired. Array, array-like, or object values

Return Value

Return ValueDescription
*New shuffled array

Underscore.js Examples

Example 1 — Official: Shuffle Numbers

Randomize a list of six numbers.

Underscore
_.shuffle([1, 2, 3, 4, 5, 6]);
// → e.g. [4, 1, 6, 3, 5, 2]
// (order varies each call)
Try It Yourself

How It Works

  1. Copy first. Underscore works on a new array.
  2. Fisher-Yates. Each index is swapped with a random earlier/equal index.
  3. Same members. You still get 1…6, just reordered.
  4. Non-deterministic. Call again for a different permutation.

Example 2 — Original Stays Intact

Prove the source array is not mutated.

Underscore
var nums = [1, 2, 3, 4, 5, 6];
var mixed = _.shuffle(nums);

console.log(nums);   // → [1, 2, 3, 4, 5, 6]
console.log(mixed);  // → shuffled copy
console.log(nums === mixed); // → false
Try It Yourself

How It Works

  1. Keep a reference. nums points at the original.
  2. Shuffle returns another array. mixed is a different object.
  3. Original order preserved. Safe for UI state you still need sorted.
  4. Objects inside. Element references are shared — only order is new.

Example 3 — Shuffle a Deck

Randomize string labels — common for games and quizzes.

Underscore
var deck = ["A♥", "K♠", "Q♦", "J♣", "10♥"];
_.shuffle(deck);
// → e.g. ["Q♦", "A♥", "10♥", "K♠", "J♣"]
Try It Yourself

How It Works

  1. Same idea as numbers. Any array elements can be shuffled.
  2. Fair deal. Fisher-Yates avoids the bias of random sort.
  3. Pair with chunk. _.chunk(_.shuffle(list), 2) makes random pairs.
  4. Need a few cards? Prefer _.sample(deck, 3).

Example 4 — Empty List & Object Values

Empty input is safe; objects become a shuffled array of values.

Underscore
_.shuffle([]);
// → []

_.shuffle({ a: 1, b: 2, c: 3 });
// → e.g. [2, 3, 1]   // values only; order varies
Try It Yourself

How It Works

  1. Empty. Nothing to permute → [].
  2. Object input. Underscore collects values, then shuffles them.
  3. Keys dropped. The result is always an array.
  4. Single item. _.shuffle([7]) → [7].

Browser & Runtime Support

_.shuffle 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

_.shuffle()

Available in [email protected] (UMD / ESM). Uses Math.random internally. Works in evergreen browsers and current Node LTS when Underscore is loaded.

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
_.shuffle 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]).

Frequently Asked Questions

_.shuffle(list) returns a shuffled copy of the list using a Fisher-Yates shuffle. Official example shape: _.shuffle([1, 2, 3, 4, 5, 6]) → e.g. [4, 1, 6, 3, 5, 2].
No. It returns a new array and leaves the original collection unchanged.
That pattern is biased. Comparison sorts are not designed for a random comparator. _.shuffle uses a proper Fisher-Yates shuffle.
_.shuffle randomizes the whole list. _.sample returns one random item, or n random items if you pass a count.
Yes. For a plain object it shuffles the object's values into a new array. Keys are not kept in the result.
Yes in theory — Fisher-Yates produces an unbiased permutation (subject to Math.random quality).

Next: _.sample()

Produce a random sample — one item, or n items from the list.

_.sample() 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