Lodash _.xor() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Array

Definition

Creates an array of unique values that is the symmetric difference of the given arrays. Shared values cancel; values unique to an odd number of arrays stay. Result order follows first appearance. Part of Lodash 4.x Array.

Pass the required arguments shown below. Lodash Array helpers usually return a new value — check whether the source array is mutated.

Syntax

Call _.xor on an array. Start with this line.

Lodash
_.xor([arrays])
CallWhat it does
_.xor([arrays])Primary signature.
_.xor(array)Call with an array argument.

Prefer import xor from "lodash/xor" when you want a smaller bundle.

Library declarations
Lodash
_.xor([arrays])

Parameters

ParameterDescription
arraysOptional. One or more arrays to inspect

Return Value

Return ValueDescription
*New array of exclusive values

Lodash Examples

Example 1 — Official: Two-Array XOR

Overlap cancels; exclusive values from each side remain.

Lodash
_.xor([2, 1], [2, 3]);
// → [1, 3]
Try It Yourself

How It Works

  1. Shared. 2 appears in both arrays → dropped.
  2. Left only. 1 is only in the first array → kept.
  3. Right only. 3 is only in the second → kept.
  4. Order. Survivors follow first-seen order: 1, then 3.

Example 2 — Three Arrays (Folded XOR)

Extra arrays keep folding: values that cancel twice may reappear oddly — here 2 and 3 cancel cleanly.

Lodash
_.xor([1, 2], [2, 3], [3, 4]);
// → [1, 4]
Try It Yourself

How It Works

  1. Step 1. xor([1,2], [2,3]) → [1, 3].
  2. Step 2. xor([1,3], [3,4]) → 3 cancels.
  3. Keep. 1 and 4 remain.
  4. Rule. Think “fold left,” not “count across all at once” as three isolated bags.

Example 3 — NaN Matches (SameValueZero)

Duplicate NaN values cancel like any other equal value.

Lodash
_.xor([NaN, 1], [NaN, 2]);
// → [1, 2]
Try It Yourself

How It Works

  1. Equality. SameValueZero treats both NaNs as equal.
  2. Cancel. They appear on both sides → dropped.
  3. Keep. 1 and 2 are exclusive → kept.
  4. Contrast. Plain === would never match NaN to NaN.

Example 4 — String Arrays

Same rules apply to string primitives.

Lodash
_.xor(["a", "b"], ["b", "c"]);
// → ["a", "c"]
Try It Yourself

How It Works

  1. Shared. "b" is in both → cancelled.
  2. Left. "a" only in the first array.
  3. Right. "c" only in the second.
  4. Result. ["a", "c"] — exclusive tags from each list.

Example 5 — Empty & Identical Arrays

Identical arrays cancel completely; XOR with empty keeps the other side’s uniques.

Lodash
_.xor([1, 2], [1, 2]);
// → []

_.xor([1, 2, 3], []);
// → [1, 2, 3]
Try It Yourself

How It Works

  1. Same lists. Every value pairs off → empty result.
  2. Empty peer. Nothing to cancel against → all of [1,2,3] survive.
  3. Still new. The result is a new array, not a shared reference.
  4. Safe. Neither case throws.

Browser & Runtime Support

_.xor is a Lodash 4.x Array helper. It runs anywhere Lodash runs — modern browsers (via CDN or bundle) and Node.js / Bun / Deno with npm install lodash.

✓ Stable · Lodash 4.x

_.xor()

Available in [email protected] full builds and the lodash/xor modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

100% With Lodash 4.x
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.xor Lodash 4.x+

Bottom line: Safe for production once Lodash is on the page or in your bundle. Prefer import xor from "lodash/xor" for smaller builds. Not a native browser API — load Lodash first.

Frequently Asked Questions

_.xor(...arrays) returns a new array of unique values that appear in an odd number of the given arrays — the symmetric difference. Shared values cancel out.
No. It returns a new array. All argument arrays stay unchanged.
difference subtracts later arrays from the first array only (directed). xor treats every array as a peer — values exclusive to any single side survive.
Lodash folds them left to right: xor(xor(a, b), c). A value survives if it ends up in an odd number of arrays after that fold.
SameValueZero — NaN matches NaN, +0 equals −0, primitives compare by value, objects by reference. Use _.xorBy or _.xorWith for custom keys or comparators.
Use import xor from "lodash/xor" or require("lodash/xor") for tree-shaking. Full build: import _ from "lodash" then _.xor.

Next: _.xorBy()

Run symmetric difference on projected keys such as id.

_.xorBy() tutorial →

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.

10 people found this page helpful