How to remove falsy values from an array in JavaScript ?

Falsy/Falsey Values: In JavaScript, there are 7 falsy values, which are given below
- false
- zero(0,-0)
- empty string(“”, ‘ ‘ , ` `)
- BigIntZero(0n,0x0n)
- null
- undefined
- NaN
In JavaScript, the array accepts all types of falsy values. Let’s see some approaches on how we can remove falsy values from an array in JavaScript:
- Using for-each loop
- Using the Array.filter method
- Using Array.reduce method
- Using for…of loop
Example:
Input: [23, 0, “gfg”, false, true, NaN, 12, “hi”, undefined, [], “”]
Output: [23, “gfg”, true, 12, “hi”, []]Input: [“”, 0, false, undefined, NaN, null]
Output: []
Approach: There are many approaches to achieve this some of them are the following:
- JavaScript for..each loop
- JavaScript Array.filter() Method
- ES6 way of Array.filter() Method
- Passing Boolean Value
- JavaScript Array.reduce() Method
- JavaScript for…of loop
JavaScript for..each loop: In this approach, we will iterate the array using the for..each loop and at every iteration, we check if the value is truthy, if it is truthy then we push the value in a newly created array, and then we return the new array.
Example: In this example, we will be using Javascript forEach() loop to remove the falsy values.
Javascript
| let arr = [23, 0, "gfg", false, true, NaN, 12, "hi", undefined, [], ""];functionremoveFalsey(arr) {    // newly created array    let newArr = [];    // Iterate the array using the forEach loop    arr.forEach((k) => {        // check for the truthy value        if(k) {            newArr.push(k);        }    });    // return the new array    returnnewArr;}console.log(removeFalsey(arr)); | 
Output:
[23, "gfg", true, 12, "hi", []]
JavaScript Array.filter() Method: In this approach, we are using the array.filter method. The filter method checks the array and filters out the false values of the array and returns a new array.
Example: In this example, we will be using the Array.filter() method to remove the false values from the array.
Javascript
| let arr = ["", 0, false, undefined, NaN, null];functionremoveFalsey(arr) {    // Applying the filter method on the array    returnarr.filter((k) => {        // Checking if the value is truthy        if(k) {            returnk;        }    });}console.log(removeFalsey(arr)); | 
Output:
[]
ES6 way of Array.filter() Method: If you can use this es6 sentence.
Example: In this example, we will use the Javascript Array.filter() method in ES6.
Javascript
| let arr = [23, 0, "gfg", false, true, NaN, 12, "hi", undefined, [], ""];functionremoveFalsey(arr) {    // Return the first parameter of the callback function    returnarr.filter((val) => val);}console.log(removeFalsey(arr)); | 
Output:
[23, "gfg", true, 12, "hi", []]
Passing Boolean Value: You can also achieve this by passing the Boolean constructor as the argument of the filter method.
Example: In this example, we will a boolean constructor in the argument of the filter method.
Javascript
| let arr = [23, 0, "gfg", false, true, NaN, 12, "hi", undefined, [], ""];functionremoveFalsey(arr) {    // Passing Boolean constructor inside filter    returnarr.filter(Boolean);}console.log(removeFalsey(arr)); | 
Output:
[23, "gfg", true, 12, "hi", []]
JavaScript Array.reduce() Method: Using the Array.reduce method we iterate the array and initialize the accumulator with an empty array and if the current value is not a falsy value then we return a concatenated value of the accumulator else we return the accumulator only.
Example: In this example, we will use the Javascript Array.reduce() method to remove the falsy values from the array.
Javascript
| let arr = [23, 0, "gfg", false, true, NaN, 12, "hi", undefined, [], ""];functionremoveFalsey(arr) {    returnarr.reduce((acc, curr) => {        // Check if the truthy then return concatenated value acc with curr.        // else return only acc.        if(curr) {            return[...acc, curr];        } else{            returnacc;        }    }, []); // Initialize with an empty array}console.log(removeFalsey(arr)); | 
Output:
[23, "gfg", true, 12, "hi", []]
JavaScript for…of loop: Using for…of loop iterate the array and check every item if it is falsy or truthy. If the item is truthy then push the item to a newly created array.
Example: In this example, we will use Javascript for..of loop to remove the falsy values from the array.
Javascript
| let arr = [23, 0, "gfg", false, true, NaN, 12, "hi", undefined, [], ""];functionremoveFalsey(arr) {    // Create a new array    let output = [];    for(x of arr) {        if(x) {            // Check if x is truthy            output.push(x);        }    }    returnoutput;}console.log(removeFalsey(arr)); | 
Output:
[23, "gfg", true, 12, "hi", []]
JavaScript for loop: Using for loop iterate the array and check every item if it is falsy or truthy. If the item is truthy then push the item to a newly created array.
Example: In this example, we will use the Javascript for loop to remove the falsy values from the array.
Javascript
| let arr = [23, 0, "gfg", false, true, NaN, 12, "hi", undefined, [], ""];functionremoveFalsey(arr) {    // Create a new array    let output = [];    for(let i = 0; i < arr.length; i++) {        if(arr[i]) {            output.push(arr[i]);        }    }    returnoutput;}console.log(removeFalsey(arr)); | 
Output:
[23, "gfg", true, 12, "hi", []]
 
				 
					


