Lodash _.difference() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Array

Definition

Creates an array of values from array that are not included in the other given arrays, using SameValueZero for equality. Order follows the first array. Since Lodash 0.1.0. Unlike _.pullAll, it returns a new array.

Pass the required arguments shown below. Lodash Array helpers usually return a new value — check whether the source array is mutated.

Syntax

Call _.difference on an array. Start with this line.

Lodash
_.difference(array, [values])
CallWhat it does
_.difference(array, [values])Primary signature.
_.difference(array)Call with an array argument.

Prefer import difference from "lodash/difference" when you want a smaller bundle.

Library declarations
Lodash
_.difference(array, [values])

Parameters

ParameterDescription
arrayRequired. Source to inspect
…valuesOptional. Arrays whose elements are excluded

Return Value

Return ValueDescription
*The value returned by _.difference().

Lodash Examples

Example 1 — Official: Basic Difference

Keep values from the first array that are missing from the second.

Lodash
_.difference([2, 1], [2, 3]);
// → [1]
Try It Yourself

How It Works

  1. Source. Start with [2, 1].
  2. Exclude. Build a deny set from [2, 3].
  3. Filter. Drop 2 (present in deny); keep 1.
  4. New array. Originals are unchanged.

Example 2 — Multiple Exclusion Arrays

All extra arrays contribute to one combined exclude set.

Lodash
_.difference([1, 2, 3, 4], [2], [4, 5]);
// → [1, 3]
Try It Yourself

How It Works

  1. Merge excludes. [2] and [4, 5] → deny {2, 4, 5}.
  2. Scan source. Walk 1, 2, 3, 4 in order.
  3. Keep. 1 and 3 are not denied.
  4. Drop. 2 and 4 match the deny set.

Example 3 — Order Follows the First Array

Survivors keep their relative order from the source.

Lodash
_.difference(["c", "a", "b", "a"], ["a"]);
// → ["c", "b"]
Try It Yourself

How It Works

  1. Exclude. Deny every "a".
  2. Both a’s go. Each matching source item is removed.
  3. Order. "c" then "b" — same order as the source.
  4. Duplicates. Difference does not unique the result by itself.

Example 4 — NaN Matches NaN

SameValueZero treats NaN as equal to itself.

Lodash
_.difference([1, NaN, 2], [NaN]);
// → [1, 2]
Try It Yourself

How It Works

  1. Exclude NaN. Deny set contains NaN.
  2. Match. Source NaN is considered equal → dropped.
  3. Keep numbers. 1 and 2 remain.
  4. Note. Plain === would not match NaN — Lodash does.

Example 5 — _.difference vs _.without

Same idea — different argument shape.

Lodash
var list = [1, 2, 3, 4];

_.difference(list, [2, 4]);   // [1, 3]
_.without(list, 2, 4);        // [1, 3]
Try It Yourself

How It Works

  1. difference. Pass an array of values to exclude.
  2. without. Pass those values as separate arguments.
  3. Same result. Both drop 2 and 4 here.
  4. Pick. Use difference when excludes already live in arrays.

Browser & Runtime Support

_.difference is a Lodash Array helper (since 0.1.0). It runs anywhere Lodash runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash.

✓ Stable · Lodash 0.1.0+

_.difference()

Available in [email protected] full builds and the lodash/difference 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
_.difference Lodash 0.1.0+

Bottom line: Safe for production once Lodash is loaded. Prefer import difference from "lodash/difference" for smaller builds. Not a native browser API — load Lodash first.

Frequently Asked Questions

_.difference(array, …values) returns a new array of values from the first array that are not included in any of the other given arrays. Equality uses SameValueZero. Order follows the first array.
No. Unlike _.pullAll, difference always returns a new array. The originals are unchanged.
_.without(array, …values) excludes individual values you list. _.difference excludes elements found inside other arrays. Pick based on whether your exclude list is already an array.
Objects are compared by reference (SameValueZero), not deep equality. Use _.differenceBy or _.differenceWith for custom comparison.
Yes. SameValueZero treats NaN as equal to NaN, so NaN in the first array is excluded if NaN appears in an exclusion array.
Use import difference from "lodash/difference" or require("lodash/difference") for tree-shaking. Full build: import _ from "lodash" then _.difference.

Next: _.differenceBy()

Compare after transforming each element with an iteratee.

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

7 people found this page helpful