Lodash _.compact() method

Beginner
⏱️ 6 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

What you’ll learn

  • What _.compact(array) removes and what it keeps.
  • How it compares to array.filter(Boolean) for everyday cleanup.
  • How to import compact without loading all of Lodash.
  • Open each numbered example in the Try it editor (?tryit=1, 2, 3) with Lodash from a CDN.
  • Edge cases such as NaN, sparse slots, and non-primitive “empty” values.

Prerequisites

Know JavaScript truthiness and arrays; optional prior read of _.chunk() on this site.

  • You understand which values are falsy in JavaScript (0, "", NaN, null, undefined, false).
  • You can run snippets in Node or use the Try-it editor in a modern browser.

Overview

_.compact returns a new array containing only the truthy elements of the input. It is a concise way to strip placeholder falsy values before validation, rendering, or persistence.

Non-destructive

The input array is unchanged; you work with a new dense list of truthy items.

Falsy cleanup

Removes the six falsy primitives you see most often in API and form payloads.

Pairs with chunk

Often used after mapping or joining steps that insert null or undefined placeholders.

Syntax

javascript
_.compact(array)
  • array: the collection to clean (array-like values are coerced).
  • Returns: a new array with only truthy values, preserving relative order.
1

Mixed truthy and falsy

Falsy entries disappear; numbers and non-empty strings remain in order.

javascript
import compact from "lodash/compact";

const cleaned = compact([0, 1, false, 2, "", 3]);
// [1, 2, 3]
Try it Yourself
2

All falsy

When every element is falsy, the result is an empty array.

javascript
import compact from "lodash/compact";

compact([false, null, 0, "", undefined, NaN]);
// []
Try it Yourself
3

Already dense

If there are no falsy values, you get a shallow copy of the same sequence.

javascript
import compact from "lodash/compact";

compact([1, 2, 3]);
// [1, 2, 3]
Try it Yourself

📋 _.compact vs filter(Boolean)

Topic_.compact.filter(Boolean)
IntentName signals “drop falsy placeholders”Generic filter with coercion to boolean
Typical primitivesSame removals for 0, "", NaN, etc.Same for most day-to-day arrays
DependencyOne Lodash importNo dependency

Pitfalls to avoid

Valid zero

Removing meaningful 0

Scores, counts, and IDs can be zero. If 0 is valid data, do not compact that array without mapping first.

Objects

{} stays

Empty objects are truthy. Use a different helper if you need to drop “empty” object records.

Sparse arrays

Holes

Prefer dense arrays; sparse behavior can surprise readers. Build lists with push or filter explicitly when holes matter.

❓ FAQ

No. Lodash returns a new array containing only the truthy elements of the input.
It drops false, null, 0, the empty string, undefined, and NaN—the same values JavaScript treats as falsy in a boolean context.
For typical primitive lists they behave the same. Differences show up with edge cases and readability; prefer compact when you explicitly mean remove Lodash falsy values.
No. An empty object {} is truthy, so it stays in the result.
_.compact([]) returns [] because there are no elements to keep.

Summary

Did you know?

_.compact removes false, null, 0, "", undefined, and NaN. It does not remove other values you might consider “empty” such as empty objects {}.

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.

6 people found this page helpful