JS Coding Questions Logo
JS Coding Questions
#363💼 Interview💻 Code

How do you remove falsy values from an array

Advertisement

728x90

You can apply the filter method on the array by passing Boolean as a parameter. This way it removes all falsy values(0, undefined, null, false and "") from the array.

javascript
1const myArray = [false, null, 1, 5, undefined];
2
3myArray.filter(Boolean); // [1, 5] // is same as myArray.filter(x => x);

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 20

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
363of476