Lodash _.union() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Array

Definition

Creates an array of unique values, in order, from all given arrays using SameValueZero for equality comparisons. Part of Lodash Array (stable in 4.x — not deprecated).

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

Syntax

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

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

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

Library declarations
Lodash
_.union([arrays])

Parameters

ParameterDescription
arraysOptional. Sources to merge; scanned in argument order

Return Value

Return ValueDescription
*New array of unique values

Lodash Examples

Example 1 — Official: Two Arrays

Merge unique values; the first occurrence wins placement.

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

How It Works

  1. First array. Official sample starts with [2] → keep 2.
  2. Next. From [1, 2], 1 is new → keep.
  3. Duplicate. Second 2 already seen → skip.
  4. Result. [2, 1] — a new array; inputs unchanged.

Example 2 — Three Arrays

Pass as many arrays as you need; later duplicates still collapse.

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

How It Works

  1. Scan left → right. Keep 2, then 1.
  2. Skip repeats. Extra 2 values are ignored.
  3. New value. 3 from the third array is appended.
  4. Rule. More arrays only add values that have not appeared yet.

Example 3 — First-Seen Order

Argument order controls where duplicates land in the result.

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

How It Works

  1. First list. Keep 3, then 1.
  2. Second list. 1 already seen → skip; 2 is new → keep.
  3. Order. Result follows discovery order, not sorted order.
  4. Tip. Put the array whose order you prefer first.

Example 4 — NaN Collapses (SameValueZero)

NaN equals NaN here — unlike strict ===.

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

How It Works

  1. Keep 1. First new value from the left array.
  2. Keep NaN. First NaN is stored.
  3. Skip second NaN. SameValueZero treats both as equal.
  4. Keep 2. New value from the right array.

Example 5 — 0 and false Stay Distinct

Falsy values are not interchangeable under SameValueZero.

Lodash
_.union([0], [false]);
// → [0, false]
Try It Yourself

How It Works

  1. Keep 0. First array contributes the number zero.
  2. Keep false. Boolean false is a different value.
  3. Not coerced. Union does not treat falsy values as the same.
  4. Contrast. Objects still compare by reference — use _.unionBy for keys.

Browser & Runtime Support

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

✓ Stable · Lodash 4.x

_.union()

Available in [email protected] full builds and the lodash/union 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
_.union Lodash 4.x+

Bottom line: Safe for production once Lodash is on the page or in your bundle. Prefer import union from "lodash/union" for smaller builds. Native Set can dedupe one list; multi-array union still needs a merge step.

Frequently Asked Questions

_.union(...arrays) creates a new array of unique values from all given arrays, using SameValueZero equality. Order follows first-seen appearance as arguments are scanned left to right.
No. It returns a new array. Each argument stays unchanged.
uniq dedupes one array. union concatenates many arrays (one level) then dedupes — like concat + uniq across arguments.
union keeps values that appear in any array. intersection keeps only values present in every array.
Use _.unionBy when equality should run on a projected key (like a property). Use _.unionWith for a custom comparator function.
Use import union from "lodash/union" or require("lodash/union") for tree-shaking friendly bundles. Full build: import _ from "lodash" then _.union.

Next: _.unionBy()

Merge arrays uniquely by projecting each element with an iteratee first.

_.unionBy() 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