Lodash _.intersection() method

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

What you’ll learn

  • How _.intersection(array, ...arrays) builds the list of values present in every supplied array.
  • Why order and uniqueness follow the first array, and how SameValueZero affects primitives like NaN.
  • When to reach for _.difference() instead (subtract sets), or _.intersectionBy / _.intersectionWith when equality is not reference or SameValueZero.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Optional reads _.initial() and _.difference(); you should be comfortable with the idea that plain objects compare by reference unless you add extra logic.

  • You understand that “in all arrays” means every argument must contain that value at least once.
  • You can run snippets in Node or open the Try-it labs in a browser.

Overview

_.intersection answers “what do these lists agree on?” Think permission lists, tag filters, or comparing IDs returned from two services—anything where membership must hold across every source.

All must match

A value survives only if it appears at least once in each argument array.

First wins order

The output is unique values ordered by their first occurrence in the first array.

New array

Inputs are read-only; you always get a fresh array reference back.

Syntax

javascript
_.intersection(array, ...arrays)
  • arrays: two or more arrays (or array-like values). Each value must exist in every argument to appear in the result.
  • Returns: a new array of unique shared values, ordered by first appearance in the first argument.
1

Two lists

Only values present in both arrays are returned, in the order they first appear in the first array.

javascript
import intersection from "lodash/intersection";

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

Three lists

Pass more arrays to tighten the filter: every extra argument is another constraint.

javascript
import intersection from "lodash/intersection";

intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]);
// [2, 1]
Try it Yourself
3

NaN and object identity

SameValueZero finds NaN across arrays; ordinary objects still require the same reference in each list.

javascript
import intersection from "lodash/intersection";

intersection([NaN, 1], [NaN, 2]);
// [NaN] — SameValueZero treats NaN as equal to NaN.

const o = { x: 1 };
intersection([o, 3], [o, 4]);
// [o] — only the same object reference counts in every array.
Try it Yourself

📋 _.intersection vs _.difference

APIQuestion it answersNote
_.intersection(a, b, …)What appears in every list?Result is a subset of the first array’s values (re-ordered uniquely).
_.difference(a, b, …)What is in the first list but not in the others?Flattens later arguments one level into one exclusion pool.

Pitfalls to avoid

0

No overlap

If nothing is shared, you get []. Double-check types (string "1" versus number 1) and reference equality for objects.

Dup

Duplicates collapse

The result is a set of unique values; repeated slots in the first array still produce at most one output entry.

Deep

No deep equality

Two different object literals with the same shape are not equal; use intersectionBy with a key or intersectionWith with a comparator when you need more than reference match.

❓ FAQ

No. Lodash returns a new array of shared values; each argument array stays unchanged.
Uniqueness and ordering follow the first array: values are emitted in the order they first appear there, provided they exist in every other argument as well.
SameValueZero, consistent with _.difference and _.indexOf: NaN matches NaN, and +0 versus -0 follows engine rules for that comparison.
difference removes anything found in the other lists. intersection keeps only what appears in all lists—including the first.
Use import intersection from "lodash/intersection" or require("lodash/intersection") for tree-shaking friendly bundles.

Summary

Did you know?

When equality should run on a projected key (for example "userId"), use _.intersectionBy from Lodash; for arbitrary comparators, _.intersectionWith.

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