Underscore.js _.union() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Array

Definition

Computes the union of the passed-in arrays: the list of unique items, in order, that are present in one or more of the arrays (official Underscore docs). Stable Array helper — not deprecated.

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

Syntax

Call _.union. Start with this line.

Underscore
_.union(*arrays)
CallWhat it does
_.union(*arrays)Primary signature.
_.union(...)Call with the arguments your case needs.

Load Underscore with import _ from "underscore" (or the UMD CDN build) so these helpers are available as _.

Library declarations
Underscore
_.union(*arrays)

Parameters

ParameterDescription
*arraysRequired. Arrays to merge; each arg must be an array

Return Value

Return ValueDescription
*New array of unique values

Underscore.js Examples

Example 1 — Official Docs Merge

The classic Underscore example — three arrays into one unique list.

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

How It Works

  1. First array. Keeps 1, 2, 3 in order.
  2. Second array. Adds new values 101 and 10; skips repeats of 2 and 1.
  3. Third array. Only 2 and 1 — both already seen, so nothing new.
  4. Result. Unique values in first-seen order: [1, 2, 3, 101, 10].

Example 2 — Merge Two Arrays

Combine two lists and drop the overlap in one call.

Underscore
var a = [1, 2, 3];
var b = [2, 3, 4];

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

How It Works

  1. Walk a. Keep 1, 2, 3.
  2. Walk b. Skip 2 and 3; keep new 4.
  3. Sources safe. a and b are unchanged.
  4. vs concat. a.concat(b) would keep duplicates; _.union does not.

Example 3 — Combine Tags From Records

Pool tags from several posts into one unique filter list.

Underscore
var post1 = ["js", "css"];
var post2 = ["css", "html"];
var post3 = ["js", "node"];

_.union(post1, post2, post3);
// → ["js", "css", "html", "node"]
Try It Yourself

How It Works

  1. Start with post1. Keep "js", "css".
  2. Add post2. Skip "css"; keep new "html".
  3. Add post3. Skip "js"; keep new "node".
  4. Use case. One call builds a combined tag cloud without nested loops.

Example 4 — Empty Input & Strict Equality

No args is safe; empty arrays add nothing; types must match.

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

How It Works

  1. No arguments. Nothing to merge → [].
  2. Empty arrays. Contribute no values; non-empty ones still merge.
  3. Strict types. Number 1 ≠ string "1", so both stay.
  4. One array. De-duplicates like _.uniq.

Browser & Runtime Support

_.union ships with Underscore.js (1.x). It runs anywhere Underscore runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install underscore.

✓ Stable · Underscore 1.x

_.union()

Available in [email protected] (UMD / ESM). Works in evergreen browsers and current Node LTS when Underscore is loaded.

100% With Underscore 1.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 Underscore 1.x+

Bottom line: Safe for production once Underscore is on the page or in your bundle. Not a native browser API — load Underscore first. Prefer a pinned CDN version (e.g. [email protected]).

Frequently Asked Questions

_.union(*arrays) merges the arrays you pass and returns a new array of unique values present in one or more of them, in first-seen order.
As many as you need. _.union is variadic — pass arrays one after another, e.g. _.union(a, b, c).
Yes. Each value appears only once. Comparison uses strict equality (===).
First-seen order: each unique value keeps the position of its first appearance, scanning left to right across the arrays.
_.uniq de-duplicates one array. _.union merges several arrays, then de-duplicates. Passing a single array to _.union behaves like _.uniq.
_.union keeps values that appear in any array. _.intersection keeps only values that appear in every array.

Next: _.intersection()

Find the values shared by every array — the overlap cousin of _.union().

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