Remove fasley values from an array using javascript

 function removeFalsy(arr) {
    let newArr = [];
    for ( let i = 0 ; i < arr.length; i++ ) {
        if( Boolean(arr[i]) === true) {
            newArr.push(arr[i]);
        }
    }
    return newArr;
  }
  //Falsy values are : false, null, 0, "", undefined, and NaN.

  console.log(removeFalsy([7, "ate", "", false, 9])); //[7, "ate", 9]

Leave a Comment

Your email address will not be published. Required fields are marked *