DEV Community

Ajay Marathe
Ajay Marathe

Posted on • Updated on

Write a function that filters out all the falsy values from a given array. (Core JS)

 const removeFalsyValues = (arr) => {
    let truthy = []

    for(let i = 0; i < arr.length; i++){
      if(arr[i]) {
        truthy.push(arr[i])
      }
    }

    return truthy;

  }

  console.log('removeFalsyValues:', removeFalsyValues([0, 1, false, 2, "", 3, undefined, NaN, null]))

Enter fullscreen mode Exit fullscreen mode

Falsy values in JavaScript are values that are considered false when evaluated in a Boolean context. These include0, false, "" (an empty string), undefined, NaN, and null.

Here's how the function works:

  • Initialize an empty array: The function starts by creating an empty array called truthy. This will be used to store the values from the original array that are not falsy.
  • Loop through the array: The function uses a for loop to go through each element in the input array arr.
  • Check if the element is truthy: Inside the loop, there's an if statement that checks if the current element (arr[i]) is truthy. If the element is truthy (meaning it's not one of the falsy values), it gets added to the truthy array.
  • Return the truthy array: After the loop has gone through all the elements, the function returns the truthy array, which now contains only the truthy values.
  • The input array is [0, 1, false, 2, "", 3, undefined, NaN, null].
  • The function will loop through each element and remove the falsy ones (0, false, "", undefined, NaN, null).
  • The remaining truthy values (1, 2, 3) are returned in a new array:[1, 2, 3].

So, the output of this code will be:removeFalsyValue [1, 2, 3].

Top comments (12)

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ
const removeFalsyValues = arr => arr.filter(x => x)
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ajaymarathe profile image
Ajay Marathe

that's cool, I just wanted to use vanilla js to explain how to write with core vanilla.js :)

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ

I used core vanilla JS

Thread Thread
 
ajaymarathe profile image
Ajay Marathe

yeah I mean without using in-built function.

Thread Thread
 
ajaymarathe profile image
Ajay Marathe

i wanted explain people how it actually works behind the scene like the iteration part and all.

Thread Thread
 
jonrandy profile image
Jon Randy πŸŽ–οΈ • Edited

Array.push is a built-in function πŸ™‚

Thread Thread
 
ajaymarathe profile image
Ajay Marathe

bro doens't my code explain in more details, like how its works and different approach :D

Collapse
 
miketalbot profile image
Mike Talbot ⭐

For the pedant in me, the function provided does not filter out falsy values from an array, instead it returns a new array that includes only the non-falsy values from the source array. The original array is unmodified.

A function which actually filters out the falsy values from an array would be:

     function removeFalsyValuesFromArray(array) {
          for(const i = array.length-1; i>=0; i--) {
              if(!array[i]) array.splice(i, 1);
          }
     }
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ajaymarathe profile image
Ajay Marathe

cool thanks for the answer, everyone has different approach. :P

Collapse
 
asmyshlyaev177 profile image
Alex • Edited

It seems like output of ChatGPT.

And can do it way simpler

const arr = [0, 1, false, 2, "", 3, undefined, NaN, null]

const filterFalsy = (arr) => arr.filter(Boolean)
console.log(filterFalsy(arr))
Enter fullscreen mode Exit fullscreen mode

On the left, click 3 dots, and "Report abuse".

Collapse
 
ajaymarathe profile image
Ajay Marathe

Why bro, why do you think it's chatgpt output, i tried with core vanilla.js, because i wanted to. everyone has different approaches :)

Collapse
 
tarunkumarkale profile image
Tarun kale

"The return false value may indicate that I haven't run any tests."

Const check=(gettting)=>{
First.filter((ele)=>{
Ele===false })

}

Let first=[1,2,3,4,0,false ]

check(first)