InterviewHubPRO
Section Overview
Q367

Question 367 of 521

Part 6: Async Internals, Memory, Microtasks & Web APIs (329 - 403)

70%
JavaScript Internals (521 Qs)•Part 6: Async Internals, Memory, Microtasks & Web APIs (329 - 403)

How do you remove falsy values from an array

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
  const myArray = [false, null, 1, 5, undefined];
  myArray.filter(Boolean); // [1, 5] // is same as myArray.filter(x => x);