JavaScript - Remove all falsy values from an array



Remove all falsy values from an array. Falsy values in JavaScript are false, null, 0, "", undefined, and NaN. Hint: Try converting each value to a Boolean.


Solution 1 boolean function

console = { 
    log: print,
    warn: print,
    error: print
};
function bouncer(arr) {
    let newArr = []
    for (let i = 0; i < arr.length; i++) {
        if (Boolean(arr[i])) {
            newArr.push(arr[i])
        }
    }
    return newArr
}
console.log(bouncer([7, "ate", "", false, 9]));

Output:


7,ate,9

Solution 2 without boolean function


console = { 
    log: print,
    warn: print,
    error: print
};
function bouncer(arr) {
    let newArray = [];
    for (var i = 0; i < arr.length; i++) {
        if (arr[i]) newArray.push(arr[i]);
    }
    return newArray;
}
console.log(bouncer([7, "ate", "", false, 9]));

Output:


7,ate,9

Solution 3 filter function


console = { 
    log: print,
    warn: print,
    error: print
};
function bouncer(arr) {
    return arr.filter(Boolean);
}
console.log(bouncer([7, "ate", "", false, 9]));

Output:


7,ate,9

download android app

Popular javascript Examples









codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube