How to Find the Difference Between Two Arrays in JavaScript
Getting the items that are only in one array is a filter-and-lookup problem: filter one array, keeping only the items that don't exist in the other. The main decision is how you check "does it exist in the other array" - a plain array scan, a Set, or one of the newer built-in Set methods.
Quick answer
const setB = new Set(b); const aOnly = a.filter(x => !setB.has(x)); is the standard fast approach - it preserves order and duplicates in a, and Set.has() is O(1) so the whole operation is roughly O(n + m). Newer JavaScript engines also support new Set(a).difference(new Set(b)) directly, though it returns a Set (order and duplicates not preserved) and needs a recent runtime.
Need the same thing in another language?
See the full reference for Python, SQL, Java, Bash, PowerShell, and more.
Filter + Set.has: the standard approach
const a = ['apple', 'banana', 'cherry', 'banana'];
const b = ['banana', 'date'];
const setB = new Set(b);
const aOnly = a.filter(x => !setB.has(x));
console.log(aOnly); // ['apple', 'cherry', 'apple'] is NOT what you get -
// actual output: ['apple', 'cherry'] since duplicate 'banana' entries are all excluded This is the pattern to reach for by default. Building setB once costs O(m), and .has() is O(1), so filtering a against it is O(n) - the whole operation is O(n + m) instead of O(n * m). It also preserves the order and any duplicates in a that aren't in b.
Symmetric difference
const a = ['apple', 'banana', 'cherry']; const b = ['banana', 'date']; const setA = new Set(a); const setB = new Set(b); const symDiff = [ ...a.filter(x => !setB.has(x)), ...b.filter(x => !setA.has(x)), ]; console.log(symDiff); // ['apple', 'cherry', 'date']
This combines A-only and B-only into a single array by running the same filter logic in both directions.
The new Set.prototype.difference() method
const a = new Set(['apple', 'banana', 'cherry']); const b = new Set(['banana', 'date']); const aOnly = a.difference(b); console.log([...aOnly]); // ['apple', 'cherry'] const symDiff = a.symmetricDifference(b); console.log([...symDiff]); // ['apple', 'cherry', 'date']
Landed in the ECMAScript 2024/2025 timeframe and shipped in recent Chrome, Firefox, and Node versions. Check current browser support before relying on it without a bundler polyfill - the manual filter approach above works everywhere and is only marginally more code.
Using lodash
import _ from 'lodash'; const a = ['apple', 'banana', 'cherry']; const b = ['banana', 'date']; const aOnly = _.difference(a, b); console.log(aOnly); // ['apple', 'cherry']
If lodash is already a dependency, _.difference() is a clean, well-tested one-liner that handles the Set optimization internally. Not worth adding lodash as a new dependency just for this - the native filter + Set approach above does the same job in three lines.
Just need the answer, not the code? Paste both lists.
Open Compare 2 Lists →Frequently Asked Questions
What is the one-liner for array difference in JavaScript?
const setB = new Set(b); const aOnly = a.filter(x => !setB.has(x)); is the standard, fast approach. It preserves the order and duplicates of a, unlike a set-based approach in some other languages.Why not just use a.filter(x => !b.includes(x))?
b.includes(x) scans the array from the start every single call, making the whole filter O(n * m). For small arrays (a few dozen items) this is fine; for anything larger, building a Set from b first keeps the lookup O(1) per item.Is there a built-in Set difference method in JavaScript?
new Set(a).difference(new Set(b)) returns a new Set with A-only items. Browser and Node support is recent (check caniuse.com for Set.prototype.difference before relying on it in production without a bundler/polyfill).How do I get the symmetric difference of two arrays in JavaScript?
new Set(a).symmetricDifference(new Set(b)). Without them, combine both directions manually: [...a.filter(x => !setB.has(x)), ...b.filter(x => !setA.has(x))].Does lodash have an array difference function?
_.difference(a, b) from lodash does exactly this (A-only, order-preserving) without writing the filter yourself, and lodash handles the Set optimization internally. It's a reasonable choice if your project already depends on lodash.Why does my array difference not work with arrays of objects?
const idsB = new Set(b.map(o => o.id)); const aOnly = a.filter(o => !idsB.has(o.id));.How do I compare two arrays case-insensitively in JavaScript?
const setB = new Set(b.map(x => x.toLowerCase())); const aOnly = a.filter(x => !setB.has(x.toLowerCase()));. This keeps the original casing in the output while matching case-insensitively.