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/difference.md.

difference es-toolkit

Returns a new array excluding elements from the first array that are in the second array.

const result = difference(firstArr, secondArr);

Usage

difference(firstArr, secondArr)

Use difference when you want to find the difference between two arrays. A new array is returned containing elements that are only in the first array and not in the second array.

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

// Find the difference of number arrays.
const array1 = [1, 2, 3, 4, 5];
const array2 = [2, 4];
difference(array1, array2);
// Returns: [1, 3, 5]
// 2 and 4 are excluded because they are in both arrays.

// Find the difference of string arrays.
const colors1 = ['red', 'blue', 'green'];
const colors2 = ['blue', 'yellow'];
difference(colors1, colors2);
// Returns: ['red', 'green']

The difference with an empty array is the same as the original array.

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

difference([1, 2, 3], []); // [1, 2, 3]
difference([], [1, 2, 3]); // []

Parameters

firstArrT[]required

The reference array to find the difference from.

secondArrT[]required

The array containing elements to exclude from the first array.

Returns

A new array containing elements that are only in the first array and not in the second array.

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.