remove falsy values
from an array
[ for loop | push ]

Remove falsy values from array

Write a function that takes an array and returns a new array with all falsy values removed, leaving only the truthy values.


In JavaScript, arrays can contain various types of values, including truthy and falsy values.

Falsy values are those that evaluate to false in a boolean context, such as ↴

false

0

"" empty string

null

undefined

NaN Not a Number

All other values are truthy

falsy values evaluate to false in a boolean context.

truthy values evaluate to true in a boolean context.


Example ...

Enter an array ...

[0, 1, 2, 3, "", "a", "b", "c", null, true, false, undefined, NaN] original array

[1, 2, 3, "a", "b", "c", true] → array with all falsy values removed

All values inside the returned array are truthy values.

Arrays are used to store multiple values in a single variable.

Each value is called an element, and each element has a numeric position in the array, known as its index.

Arrays are zero-indexed, meaning the first element is at index 0, the second at index 1, and so on.

Arrays can contain any data type, including numbers, strings, and objects.

const arr1 = [2, 4, 6]; array

arr1[0]; element at index 0 → 2

arr1[1]; element at index 1 → 4

arr1[2]; element at index 2 → 6

arr1[3]; element at index 3 → undefined index not found


Remove falsy values from an array using ↴

for loop → executes a block of code a number of times.

push() method → adds specified elements to the end of an array and returns the new length of the array.


for loop repeatedly executes a block of code until a specified condition evaluates to false.

The loop runs a block of code a set number of times, defined by an initialization, a condition, and an increment.

for (let x = 0; x < 4; x++) {

console.log(x);

}

Loop variable x is initialized to 0

Condition x < 4 is checked before each iteration.

The loop will continue to run as long as x is less than 4

The loop repeatedly executes a block of code 4 times, from 0 to 3

For each iteration of the loop, the current value of x is printed to the console.

After each iteration, x is incremented by 1 x++

When x reaches 4 the condition evaluates to false, terminating the loop.

0

1

2

3 → printed to console


push() method adds new elements to the end of an array.

Add 4 to end of array.

const arr2 = [1, 2, 3];

arr2.push(4);

console.log(arr2); returns ↴

[1, 2, 3, 4]4 added to end of array

The push() method changes the length of the array.

arr2 is modified.

Using the spread operator creates a new array.

Add 4 to a new array.

const arr3 = [1, 2 , 3];

const arr4 = [...arr3, 4];

console.log(arr4); returns ↴

[1, 2, 3, 4]4 added to new array

console.log(arr3); returns ↴

[1, 2 ,3]

arr3 remains unchanged.


Initialize a variable to hold the array to remove falsy values from.

const array1 = [0, 1, 2, 3, "", "a", "b", "c", null, true, false, undefined, NaN]; → user input


Define a function removeFalsy to remove falsy values from array.

function removeFalsy(arr) {}

The function takes an array as input arr and returns a new array with all falsy values removed, leaving only the truthy values.

Create an empty array to hold truthy values.

const arrTruthy = [] arrTruthy

Loop through each element in the input array arr

for (let x = 0; x < arr.length; x++) {}

Check if the current element arr[x] is truthy.

if (arr[x]) {}

If truthy, add it to the new array.

arrTruthy.push(arr[x])

Return the new array containing only truthy values.

return arrTruthy


Call the function with ↴

removeFalsy(array1);


Remove falsy values from an array.

const array1 = [0, 1, 2, 3, "", "a", "b", "c", null, true, false, undefined, NaN];

function removeFalsy(arr) {

const arrTruthy = [];

for (let x = 0; x < arr.length; x++) {

if (arr[x]) {

arrTruthy.push(arr[x]);

}

}

return arrTruthy;

}

call function

removeFalsy(array1); returns ↴

[1, 2, 3, "a", "b", "c", true]

Remove falsy values from an array