For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /reference/array/intersection.md.

intersection es-toolkit

Returns a new array containing elements that are commonly included in both arrays.

const result = intersection(firstArr, secondArr);

Usage

intersection(firstArr, secondArr)

Use intersection when you want to find only common elements in two arrays. It returns a new array containing only elements from the first array that also exist in the second array. This is useful for finding the intersection of two data sets.

import { intersection } from 'es-toolkit/array';

// Find the intersection of number arrays
const numbers1 = [1, 2, 3, 4, 5];
const numbers2 = [3, 4, 5, 6, 7];
intersection(numbers1, numbers2);
// Returns: [3, 4, 5]

// Find the intersection of string arrays
const strings1 = ['apple', 'banana', 'cherry'];
const strings2 = ['banana', 'cherry', 'date'];
intersection(strings1, strings2);
// Returns: ['banana', 'cherry']

It also handles cases with no intersection or special cases.

import { intersection } from 'es-toolkit/array';

// Returns an empty array when there's no intersection
const noCommon1 = [1, 2, 3];
const noCommon2 = [4, 5, 6];
intersection(noCommon1, noCommon2);
// Returns: []

// Also returns an empty array when one side is empty
const numbers = [1, 2, 3];
const empty: number[] = [];
intersection(numbers, empty);
// Returns: []

Parameters

firstArrreadonly T[]required

The first array to compare.

secondArrreadonly T[]required

The second array to compare.

Returns

Returns a new array containing elements commonly included in both arrays.

T[]
Source: es-toolkit

Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.