javascript

Remove empty, false, undefined values from array Javscript

If you want to remove all false, empty, undefined values from the Javascript array you can use the code example and methods explained in this post.

var my_arr = [4, '', 0, 10, 7, '', false, 10];

my_arr = my_arr.filter(Boolean);

console.log(my_arr);

Output

[4,10,7,10]

Try it yourself

Use For Loop and ES6 to remove falsy values from an Array

If you are using ES6 in your project, you can easily remove empty or falsy values from an array. It is the fastest way to do that.

const input_arr = [20, 30, , , , null, 50, 40, undefined, , null, undefined];
const result = [];

for (let i of input_arr)
    i && result.push(i);

console.log(result)

Output

[20,30,50,40]

Try it yourself

Was this helpful?