Table of contents
1.
Introduction 
2.
Why This Function is Used
3.
Syntax, Parameter, and Return Value
3.1.
Syntax
3.2.
Parameters
3.3.
Return Value
4.
Examples of Lodash _.compact() Method
4.1.
Example 1: Removing Falsy Values
4.2.
JavaScript
4.3.
Example 2: Compact on Array with No Falsy Values
4.4.
JavaScript
4.5.
Example 3: Compact on Empty Array 
4.6.
JavaScript
4.7.
Example 4: Compact on Array with Complex Elements
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
Does _.compact() affect the original array?
5.2.
Can _.compact() remove custom-defined falsy values?
5.3.
Is _.compact() suitable for all data types?
6.
Conclusion
Last Updated: Aug 13, 2025
Easy

Lodash _.compact() Method

Author Gaurav Gandhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

In the realm of JavaScript programming, the Lodash library offers a suite of powerful tools, with the _.compact() method being a particularly useful array utility. This method streamlines the process of cleaning arrays, efficiently removing all falsy values. Falsy values in JavaScript include elements like false, null, 0, "", undefined, and NaN. 

Lodash _.compact() Method

The _.compact() method is a straightforward yet impactful function, especially valuable in data cleaning and processing scenarios.

Why This Function is Used

The _.compact() method is utilized for:

  • Data Cleansing: It effortlessly filters out falsy values from an array, ensuring that only truthy values remain. This is essential in data processing where accuracy and cleanliness of data are paramount.
     
  • Simplifying Code: It eliminates the need for complex filtering logic, thereby making the code more concise and readable.
     
  • Optimizing Data Handling: By removing non-essential elements, _.compact() enhances the efficiency of data handling, especially in large datasets.

This method is a go-to for developers who need to ensure their arrays contain only valid, meaningful data.

Syntax, Parameter, and Return Value

The syntax of the _.compact() method is simple and intuitive:

Syntax

_.compact(array)

Parameters

array (Array): The array to be compacted.

Return Value

Array: Returns a new array with all falsy values removed.

Unlike some array methods, _.compact() does not modify the original array; it produces a new array with the filtered content.

Examples of Lodash _.compact() Method

Example 1: Removing Falsy Values

  • JavaScript

JavaScript

const _ = require('lodash');

const mixedArray = [0, 1, false, 2, '', 3];
const compactedArray = _.compact(mixedArray);

console.log(compactedArray);
You can also try this code with Online Javascript Compiler
Run Code

 Output:

 [1, 2, 3]


This example illustrates the removal of falsy values like 0, false, and '', leaving only the truthy values.

Example 2: Compact on Array with No Falsy Values

  • JavaScript

JavaScript

const truthyArray = [1, 2, 3, 4];
const resultArray = _.compact(truthyArray);

console.log(resultArray);
You can also try this code with Online Javascript Compiler
Run Code

Output: 

[1, 2, 3, 4]


Here, an array with no falsy values remains unchanged after compacting.

Example 3: Compact on Empty Array 

  • JavaScript

JavaScript

const emptyArray = [];
const compactedEmpty = _.compact(emptyArray);

console.log(compactedEmpty);
You can also try this code with Online Javascript Compiler
Run Code

Output:

 []


Compacting an empty array results in an empty array, demonstrating the method's consistency.

Example 4: Compact on Array with Complex Elements

  • JavaScript

JavaScript

const complexArray = [null, { name: 'Alice' }, undefined, [1, 2, 3]];
const compactedComplex = _.compact(complexArray);

console.log(compactedComplex);
You can also try this code with Online Javascript Compiler
Run Code

Output:

 [{ name: 'Alice' }, [1, 2, 3]]


In this example, _.compact() effectively removes null and undefined, handling complex data types seamlessly.

Frequently Asked Questions

Does _.compact() affect the original array?

No, _.compact() does not modify the original array. It returns a new array with the falsy values removed.

Can _.compact() remove custom-defined falsy values?

_.compact() is designed to remove standard JavaScript falsy values only. Custom-defined falsy values require a different approach, like a custom filter function.

Is _.compact() suitable for all data types?

Yes, _.compact() works with any data type that JavaScript considers falsy. It is versatile and can handle arrays containing various data types.

Conclusion

The _.compact() method in Lodash is a simple yet powerful tool for cleaning arrays by removing falsy values. Its ability to streamline data and simplify coding makes it an invaluable function in JavaScript programming, particularly in scenarios involving data processing and manipulation. By ensuring arrays contain only meaningful elements, _.compact() significantly enhances data integrity and application performance.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass