- Published on
How to filter falsy values in Javascript Array
- Authors
- Name
- Ashik Nesin
- @AshikNesin
If you have an array with mixed values and want to filter falsy values like false, undefined, null, 0, NaN, '' (empty string)
then you can easily do it using the Array filter.
Here's how to do it:
const exampleArray = [1, false, undefined, "hello"]
const truthlyArray = exampleArray.filter(Boolean) // [1, 'hello']
We're passing Boolean
as a first parameter instead of writing our function to filter it. And it'll take care of filtering falsy values
Happy filtering values!