Lodash _.compact() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Array

Definition

Creates an array with all falsy values removed. The values false, null, 0, "", undefined, and NaN are falsy. Part of Lodash Array since 0.1.0.

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

Syntax

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

Lodash
_.compact(array)
CallWhat it does
_.compact(array)Primary signature.
_.compact(array)Call with an array argument.

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

Library declarations
Lodash
_.compact(array)

Parameters

ParameterDescription
arrayRequired. Source to clean
Extra argsOptional. None — compact has no size/predicate

Return Value

Return ValueDescription
*New array of truthy values

Lodash Examples

Example 1 — Official: Remove Falsy Values

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

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

How It Works

  1. Scan. Walk each element in order.
  2. Drop falsy. 0, false, and "" fail the truthy check.
  3. Keep truthy. 1, 2, and 3 are pushed to the result.
  4. New array. The source list is unchanged.

Example 2 — null, undefined, and NaN

These three are also falsy and get removed.

Lodash
_.compact([null, "ok", undefined, NaN, "done"]);
// → ["ok", "done"]
Try It Yourself

How It Works

  1. null / undefined. Both are falsy → dropped.
  2. NaN. Also falsy in a boolean check → dropped.
  3. Strings. Non-empty strings are truthy → kept.
  4. Order. Surviving values keep their relative order.

Example 3 — Values That Stay

{}, [], and "0" are truthy — compact does not remove them.

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

How It Works

  1. Objects / arrays. Empty {} and [] are still truthy.
  2. String zero. "0" is a non-empty string → kept.
  3. Number zero. 0 is falsy → dropped.
  4. Lesson. Compact follows JS truthiness, not “looks empty.”

Example 4 — Empty Array

Nothing to keep → empty result.

Lodash
_.compact([]);
// → []
Try It Yourself

How It Works

  1. Empty source. Length is 0.
  2. Loop skips. No elements are copied.
  3. Result. A new empty array [].
  4. Safe. No throw — common after filters that cleared everything.

Example 5 — _.compact vs filter(Boolean)

Same idea for typical lists — compact names the intent clearly.

Lodash
var list = [0, "a", false, "b"];

_.compact(list);           // ["a", "b"]
list.filter(Boolean);      // ["a", "b"]
Try It Yourself

How It Works

  1. Same rule. Both keep values that are truthy in JS.
  2. compact. Dedicated Lodash helper — clear intent in Lodash codebases.
  3. filter. Native alternative with no Lodash dependency.
  4. Pick one. Prefer _.compact when you already use Lodash array helpers.

Browser & Runtime Support

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

✓ Stable · Lodash 0.1.0+

_.compact()

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

Bottom line: Safe for production once Lodash is loaded. Prefer import compact from "lodash/compact" for smaller builds. Not a native browser API — load Lodash first.

Frequently Asked Questions

_.compact(array) creates a new array with all falsy values removed. The falsy values are false, null, 0, "", undefined, and NaN.
No. It returns a new array of truthy elements. The source array is left unchanged.
For typical lists they match. _.compact makes the intent clear — remove Lodash/JS falsy values — and is a small dedicated helper.
No. {} and [] are truthy in JavaScript, so they stay. Only the six falsy values listed above are dropped.
_.compact([]) returns []. There is nothing to keep.
Use import compact from "lodash/compact" or require("lodash/compact") for tree-shaking. Full build: import _ from "lodash" then _.compact.

Next: _.concat()

Learn how to join arrays and values into one new array.

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

9 people found this page helpful