Write a function that filters out all the falsy values from a given array.

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, …


This content originally appeared on DEV Community and was authored by Ajay Marathe

 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]))

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].


This content originally appeared on DEV Community and was authored by Ajay Marathe


Print Share Comment Cite Upload Translate Updates
APA

Ajay Marathe | Sciencx (2024-08-10T22:27:12+00:00) Write a function that filters out all the falsy values from a given array.. Retrieved from https://www.scien.cx/2024/08/10/write-a-function-that-filters-out-all-the-falsy-values-from-a-given-array/

MLA
" » Write a function that filters out all the falsy values from a given array.." Ajay Marathe | Sciencx - Saturday August 10, 2024, https://www.scien.cx/2024/08/10/write-a-function-that-filters-out-all-the-falsy-values-from-a-given-array/
HARVARD
Ajay Marathe | Sciencx Saturday August 10, 2024 » Write a function that filters out all the falsy values from a given array.., viewed ,<https://www.scien.cx/2024/08/10/write-a-function-that-filters-out-all-the-falsy-values-from-a-given-array/>
VANCOUVER
Ajay Marathe | Sciencx - » Write a function that filters out all the falsy values from a given array.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/10/write-a-function-that-filters-out-all-the-falsy-values-from-a-given-array/
CHICAGO
" » Write a function that filters out all the falsy values from a given array.." Ajay Marathe | Sciencx - Accessed . https://www.scien.cx/2024/08/10/write-a-function-that-filters-out-all-the-falsy-values-from-a-given-array/
IEEE
" » Write a function that filters out all the falsy values from a given array.." Ajay Marathe | Sciencx [Online]. Available: https://www.scien.cx/2024/08/10/write-a-function-that-filters-out-all-the-falsy-values-from-a-given-array/. [Accessed: ]
rf:citation
» Write a function that filters out all the falsy values from a given array. | Ajay Marathe | Sciencx | https://www.scien.cx/2024/08/10/write-a-function-that-filters-out-all-the-falsy-values-from-a-given-array/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.