JavaScript: Remove null, 0, blank, false, undefined and NaN values from an array
JavaScript Array: Exercise-24 with Solution
Remove Falsy Values
Write a JavaScript function to remove. 'null', '0', '""', 'false', 'undefined' and 'NaN' values from an array.
Sample array: [NaN, 0, 15, false, -22, '',undefined, 47, null]
Expected result:
[15, -22, 47]
Visual Presentation:

Sample Solution:
JavaScript Code:
Output:
[15,-22,47]
Flowchart:

ES6 Version:
Live Demo:
For more Practice: Solve these Related Problems:
- Write a JavaScript function that removes false, null, 0, "", undefined, and NaN values from an array using the filter() method.
- Write a JavaScript function that iterates over an array and retains only truthy values.
- Write a JavaScript function that creates a new array containing only truthy elements from the input array.
- Write a JavaScript function that validates the input array and returns a clean array of truthy values.
Improve this sample solution and post your code through Disqus.
Previous: Write a JavaScript function to find the difference of two arrays.
Next: Write a JavaScript function to sort the following array of objects by title value.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.