Tuesday, November 25, 2025
HomeLanguagesJavascriptUnderscore.js _.compact() Function

Underscore.js _.compact() Function

The _.compact() function is an inbuilt function in Underscore.js library of JavaScript which is used to return an array after removing all the false values. The false values in JavaScript are NaN, undefined, false, 0, null or an empty string. It’s output is an array containing all the even values like the elements of the array, numbers, alphabets, characters, true, etc.

Syntax: 

_.compact( list ) 

Parameters: This function contains single parameter list which holds the array containing the true and false elements.
Return value: It returns an array containing only true values.

Passing a list of both the true and the false elements to _.compact() function: The _.compact() function start by taking the elements one by one and then checks whether it is a false element or not. If it is false element then it just ignores that element. Otherwise it adds the true element to the resultant array. Here the false elements represented as false and an empty string represented by ”.

Example:  

HTML




<!DOCTYPE html>
<html>
    <head>
        <script src
        </script>
    </head>
    <body>
        <script type="text/javascript">
            console.log(_.compact([0, 1, false, 2, '', 3]));
        </script>
    </body>
</html>                    


Output: 
 

Passing a list containing all the false values to the _.compact() function: If passed array contains all the false elements then the _.compact() function will work the same. It will check each element and since they are all false so all the elements will be ignored. So, the resultant array formed will not have any element and it’s length will be 0.

Example: 

HTML




<!DOCTYPE html>
<html>
    <head>
        <script src
        </script>
    </head>
    <body>
        <script type="text/javascript">
            console.log(_.compact([0, false, '', undefined, NaN]));
        </script>
    </body>
</html>                    


Output: 
 

Passing a list which contains a false element in ” to _.compact() function: Pass a false element, undefined inside ” as ‘undefined’. Though this is a false element but since it is given inside ” therefore it is treated as a character element. Hence, it is no longer a false element. Rest it works the same as above.

Example: 

HTML




<!DOCTYPE html>
<html>
    <head>
        <script src
        </script>
    </head>
    <body>
        <script type="text/javascript">
            console.log(_.compact([false, 'HTML', NaN,
                       'CSS', 'undefined']));
        </script>
    </body>
</html>                    


Output:

Passing a list containing modified false values to the _.compact() function: The array contains an element as true which is included in the resultant array. The ‘no’ element is also included as it is inside ” which makes it a character. Also if pass ‘no2’ it is also not ignored by the _.compact() function.

Example:  

HTML




<!DOCTYPE html>
<html>
    <head>
        <script src
        </script>
    </head>
    <body>
        <script type="text/javascript">
            console.log(_.compact([false, true, 'yes', 'no', "no2"]));
        </script>
    </body>
</html>                    


Output: 
 

Note: These commands will not work in Google console or in Firefox as for these additional files need to be added which they didn’t have added. So, add the given links to your HTML file and then run them. 

HTML




<script type="text/javascript" src =  
</script


RELATED ARTICLES

Most Popular

Dominic
32412 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6790 POSTS0 COMMENTS
Nicole Veronica
11934 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12000 POSTS0 COMMENTS
Shaida Kate Naidoo
6910 POSTS0 COMMENTS
Ted Musemwa
7169 POSTS0 COMMENTS
Thapelo Manthata
6868 POSTS0 COMMENTS
Umr Jansen
6856 POSTS0 COMMENTS