Remove all falsy values from an array in JavaScript
This post will discuss how to remove all falsy values from an array in JavaScript. The solution should return a new array that excludes all falsy values.
There are 7 falsy values in JavaScript – false, zero (0), BigInt (0n), empty string ("", '', ``), null, undefined, and NaN. An array in JavaScript permits all types of falsy values. There are several methods to filter falsy values from an array, which are covered below:
1. Boolean constructor
The recommended solution is to pass the Boolean constructor to the filter() method to filter out all the’ falsy’ values from the array. This works since the Boolean constructor returns false on any falsy value and vice versa.
|
1 2 3 4 5 6 7 8 |
var arr = [ 0, 1, '', undefined, false, 2, undefined, null, , 3, NaN ]; var filtered = arr.filter(Boolean); console.log(filtered); /* Output: [ 1, 2, 3 ] */ |
2. Using !!x Expression
Alternatively, you can use the !!x expression, which returns false on any falsy value. This is demonstrated below:
|
1 2 3 4 5 6 7 8 |
var arr = [ 0, 1, '', undefined, false, 2, undefined, null, , 3, NaN ]; var filtered = arr.filter(x => !!x); console.log(filtered); /* Output: [ 1, 2, 3 ] */ |
3. Using x => x
Here’s another simple way to filter a falsy value from an array:
|
1 2 3 4 5 6 7 8 |
var arr = [ 0, 1, '', undefined, false, 2, undefined, null, , 3, NaN ]; var filtered = arr.filter(x => x); console.log(filtered); /* Output: [ 1, 2, 3 ] */ |
Sometimes you want to retain the value 0 and remove all other falsy values present in the array. You can achieve this as:
|
1 2 3 4 5 6 7 8 |
var arr = [ 0, 1, '', undefined, false, 2, undefined, null, , 3, NaN ]; var filtered = arr.filter(e => (e === 0 || e)); console.log(filtered); /* Output: [ 0, 1, 2, 3 ] */ |
4. Using Underscore/Lodash Library
Similar to the Array.prototype.filter() method, Underscore and Lodash Library provide their implementation of the filter() method. You can pass any of the above callback functions to the _.filter method and get the same result.
|
1 2 3 4 5 6 7 8 9 |
var _ = require('underscore'); // or, use Lodash var arr = [ 0, 1, '', undefined, false, 2, undefined, null, , 3, NaN ]; var filtered = _.filter(arr, Boolean); console.log(filtered); /* Output: [ 1, 2, 3 ] */ |
5. Using jQuery
In jQuery, you can use the $.grep() method, which removes items from an array that fails the specified condition. The following example demonstrates.
|
1 2 3 4 5 6 7 8 9 10 11 |
const { JSDOM } = require("jsdom"); const { window } = new JSDOM(); var $ = require("jquery")(window); var arr = [ 0, 1, '', undefined, false, 2, undefined, null, , 3, NaN ]; var filtered = $.grep(arr, Boolean); console.log(filtered); /* Output: [ 1, 2, 3 ] */ |
That’s all about removing all empty values from an array in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)