Easy

Remove Falsy Values from an Array

Description

In this challenge, you need to write a function that removes all falsy values from an array and returns a new array with only truthy values.

Falsy values in JavaScript include:

  • false
  • 0 (zero)
  • "" (empty string)
  • null
  • undefined
  • NaN (Not a Number)

This task is useful when cleaning up data, such as filtering out empty or invalid values in form submissions, API responses, or datasets.

Example Usage

Constraints

  • The input will always be an array of various data types.
  • The function should return a new array with only truthy values.
  • The input array may contain numbers, strings, booleans, 'null', 'undefined', or 'NaN'.
  • If all values are falsy, return an empty array.

window code 2Test Cases

Input:

null

Expected Output:

[
  "NaN",
  "Hello",
  "null",
  42
]