Lodash _.difference() method

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

What you’ll learn

  • How _.difference(array, ...values) builds a new array without values that appear in any later argument.
  • How exclusion lists from multiple arrays are merged before filtering the first array.
  • Equality rules (SameValueZero) and when to prefer differenceBy / differenceWith.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Comfort with arrays and filter; optional prior reads _.concat() and the array methods hub.

  • You understand that comparing objects with === uses identity, not deep equality.
  • You can run snippets in Node or open the Try-it labs in a browser.

Overview

_.difference keeps only values from the first array that are not found in the combined elements of every following argument. Order and duplicates from the first array are preserved unless they match an excluded value.

Non-destructive

All inputs stay unchanged; you always get a fresh array instance.

Subtract sets

Typical for “IDs still allowed” after removing blocked IDs, completed tasks, or merged branches.

Shallow equality

Primitives use SameValueZero; objects match only when references are the same.

Syntax

javascript
_.difference(array, [values])
  • array: source collection to filter (array-like values are coerced).
  • values: zero or more arrays (or array-like values); each is flattened one level and unioned into the exclusion set.
  • Returns: a new array of values from array not present in that exclusion set.
1

Basic exclusion

Elements from the first array that also appear in the second are removed; remaining order follows the first array.

javascript
import difference from "lodash/difference";

difference([2, 1], [2, 3]);
// [1]
Try it Yourself
2

Several exclusion arrays

Every later argument contributes values to exclude. The result is everything left in the first array after removing that combined set.

javascript
import difference from "lodash/difference";

difference([1, 2, 3, 4, 5], [5, 2], [4]);
// [1, 3]
Try it Yourself
3

Duplicates and order preserved

difference is not uniq: if a value appears twice in the first array and is not excluded, both copies remain in order.

javascript
import difference from "lodash/difference";

difference([1, 2, 1, 2], [2]);
// [1, 1]
Try it Yourself

📋 _.difference vs _.without

APIBest whenNote
_.difference(a, b, c)Exclusions already live in one or more arrays you can pass throughLater arguments are flattened one level, then unioned
_.without(a, v1, v2)You have a short list of literal values to dropVariadic values, not “array of exclusions” as a single rest spread of nested arrays

Pitfalls to avoid

Objects

Expecting deep equality

Two different object literals with the same keys are still two references. They will not cancel each other in difference.

Depth

Shallow flatten on exclusions

Later arguments are flattened only one level before building the exclusion set. Deeply nested lists of IDs may need flatten, flatMap, or a dedicated pipeline before calling difference.

XOR

Symmetric difference

difference is directional: only the first array is filtered. For symmetric set math across all inputs, see _.xor.

❓ FAQ

No. Lodash returns a new array; the originals are unchanged.
Each argument after the first is flattened one level, then all of those values form one exclusion set. If any value appears in that set, every matching element is removed from the first array in the result.
Similar goal, different API. _.without() takes individual values to omit. difference takes additional arrays (or array-like values) whose elements are excluded. Use whichever matches the shape of your data.
Use import difference from "lodash/difference" or require("lodash/difference") for tree-shaking friendly bundles.
Ordinary objects are compared by reference, not by deep structural equality. Use differenceBy or differenceWith if you need another comparison rule.

Summary

Did you know?

_.difference uses SameValueZero equality (like _.indexOf), so NaN can match NaN. For custom equality (for example deep object compares), use _.differenceWith or map first with _.differenceBy.

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