Same rule. Both keep values that are truthy in JS.
compact. Dedicated Lodash helper — clear intent in Lodash codebases.
filter. Native alternative with no Lodash dependency.
Pick one. Prefer _.compact when you already use Lodash array helpers.
Compatibility
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 ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
_.compactLodash 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.