Lodash _.xor() method

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

What you’ll learn

  • How _.xor(...arrays) keeps elements that appear exclusively after symmetric difference folding.
  • Why duplicates toggle membership counts and can cancel unexpectedly.
  • When _.difference(), _.union(), _.intersection(), or _.xorBy() expresses the intent more directly.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Know _.difference() subtracts later arrays from the first operand—xor removes that directional bias entirely.

  • You can read Venn diagrams with exclusive regions shaded.
  • You can open Try-it labs or run snippets locally.

Overview

_.xor helps diff feature flags, reconcile polling snapshots, or isolate IDs that flipped between imports—anything where neither side should dominate the filter.

Symmetric cut

Shared keys cancel; leftovers from any operand bubble outward.

Toggle semantics

Repeated occurrences flip parity—expect surprises when duplicates abound.

Immutable inputs

Sources remain reusable for downstream merges or auditing.

Syntax

javascript
_.xor(...arrays)
  • arrays: two or more collections (array-like arguments flatten predictably like other Lodash set helpers).
  • Returns: new array containing symmetric-difference survivors ordered by Lodash merge rules.
1

Classic two-array exclusive merge

Overlapping entries cancel—only numbers unique to a single operand survive.

javascript
import xor from "lodash/xor";

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

Three-way symmetric difference

Each additional array continues folding XOR logic—elements appearing across lists an odd number of times survive.

javascript
import xor from "lodash/xor";

xor([1, 2], [2, 3], [3, 4]);
// → [1, 4]
Try it Yourself
3

NaN parity like other values

Because Lodash uses SameValueZero, duplicate NaN placeholders behave predictably when aligning noisy datasets.

javascript
import xor from "lodash/xor";

xor([NaN, 1], [NaN, 2]);
// → [1, 2]
Try it Yourself

📋 _.xor vs difference, union, intersection

APIShapeUse case
_.xor(...arrays)Symmetric differenceExclusive-or filtering across peers
_.difference(array, ...others)Directed subtractionPrimary feed minus blacklist arrays
_.union(...arrays)Coverage unionKeep every distinct element ever seen
_.intersection(...arrays)Overlap coreRequire presence in every operand

Pitfalls to avoid

Dup

Duplicate toggling

Repeated identical values inside one array flip parity—run _.uniq() first when you intend pure set semantics.

Dir

Expecting directionality

Need “everything in A unless also in B”? That is difference, not xor.

Deep

Structural equality

Distinct object literals never cancel—reach for xorBy or xorWith when projections or comparators matter.

❓ FAQ

No. Lodash returns a fresh array; originals remain untouched.
SameValueZero—NaN matches NaN, +0 matches −0, primitives compare by value, objects compare by reference unless you switch to xorBy/xorWith.
Lodash folds arguments together so elements appearing an odd number of times across the combined multiset survive; even counts cancel out.
difference removes anything present in later arrays from the first array only. xor has no privileged left operand—anything exclusive to any single contributor survives.
Use import xor from "lodash/xor" or require("lodash/xor") for tree-shaken bundles.

Summary

Did you know?

Symmetric difference answers “what appears in an odd number of lists?” for membership toggling—pair it mentally with _.difference() when you only subtract from a primary array.

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