Underscore.js _.compact() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Array

Definition

Returns a copy of the list with all falsy values removed (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 _.compact. Start with this line.

Underscore
_.compact(list)
CallWhat it does
_.compact(list)Primary signature.
_.compact(...)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
_.compact(list)

Parameters

ParameterDescription
listRequired. Array (or list) to clean

Return Value

Return ValueDescription
*New array of truthy values

Underscore.js Examples

Example 1 — Official: Remove Falsy Values

Drop 0, false, and ""; keep the numbers that remain.

Underscore
_.compact([0, 1, false, 2, "", 3]);
// → [1, 2, 3]
Try It Yourself

How It Works

  1. Scan left to right. Each item is tested for truthiness.
  2. Drop falsy. 0, false, and "" are removed.
  3. Keep truthy. 1, 2, and 3 stay, in order.
  4. New array. The source list is not modified.

Example 2 — All Six Falsy Values

Every JavaScript falsy value is stripped in one pass.

Underscore
_.compact([false, null, 0, "", undefined, NaN, "ok", 7]);
// → ["ok", 7]
Try It Yourself

How It Works

  1. Know the six. false, null, 0, "", undefined, NaN.
  2. Remove them all. Each fails the truthy check.
  3. Keep the rest. "ok" and 7 survive.
  4. Tip. NaN is falsy even though typeof NaN === "number".

Example 3 — Truthy Values That Survive

Empty objects, empty arrays, and the string "0" are kept.

Underscore
_.compact([0, "0", false, "false", [], {}, null]);
// → ["0", "false", [], {}]
Try It Yourself

How It Works

  1. Drop real falsy. 0, false, and null go away.
  2. Strings are truthy. "0" and "false" are non-empty strings.
  3. Objects/arrays. [] and {} are always truthy in JavaScript.
  4. Need custom rules? Use _.filter.

Example 4 — Empty Array & All Falsy

Nothing truthy left means an empty result — never an error.

Underscore
_.compact([]);
// → []

_.compact([false, 0, "", null, undefined, NaN]);
// → []
Try It Yourself

How It Works

  1. Empty source. No items → [].
  2. All falsy. Every item is dropped → still [].
  3. Safe. No throw; loops over the result simply do nothing.
  4. Check length. Use result.length if you must know whether anything survived.

Browser & Runtime Support

_.compact 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

_.compact()

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
_.compact 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

_.compact(list) returns a copy of the list with all falsy values removed, keeping only truthy elements.
The six JavaScript falsy values: false, null, 0, "" (empty string), undefined, and NaN. Everything else is kept.
No. [] and {} are truthy in JavaScript, so they survive. So do non-empty strings like "0" and "false".
An empty array []. An array made entirely of falsy values also returns [], never an error.
_.compact is a shorthand for removing falsy values with no predicate. Use _.filter when you need a custom condition.
No. It returns a new array; the original is left unchanged.

Next: _.flatten()

Flatten one or more levels of nested arrays into a single list.

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