Last updated: Mar 1, 2024
Reading timeยท6 min
To remove all null values from an array:
Array.filter()
method to iterate over the array.null
.filter()
method returns a new array containing only the elements that
satisfy the condition.// โ Remove null values from an array const arr = [ 'one', null, 'two', 0, null, undefined, 'three', null, ]; const results = arr.filter(element => { return element !== null; }); console.log(results); // ๐๏ธ [ 'one', 'two', 0, undefined, 'three' ] // ----------------------------------------------------------- // โ Remove null and undefined values from an array const arr2 = ['a', , 'b', , undefined, 0, 'c', null]; const results2 = arr2.filter(element => { return element !== null && element !== undefined; }); console.log(results2); // ๐๏ธ [ 'a', 'b', 0, 'c' ]
The same approach can be used to only remove the undefined
values from an
array.
// โ Remove all undefined values from an array const arr = ['a', undefined, 'b', undefined, 'c', undefined]; const results = arr.filter(element => { return element !== undefined; }); console.log(results); // ๐๏ธ ['a', 'b', 'c']
The function we passed to the Array.filter() method gets called with each element in the array.
results
array.We explicitly check if each element is not equal to null to only add non-null elements to the new array.
filter()
method doesn't change the contents of the original array. It returns a new array that contains only the elements that satisfy the condition.Something we often have to do is remove all falsy
values from an array.
Falsy values in JavaScript are: false
, 0
, ""
, null
, undefined
, NaN
.
To remove all falsy values from an array:
Array.filter()
method to iterate over the array.filter()
method will return a new array that only contains the truthy
values of the original array.// โ Remove all falsy values from an array const arr = [ null, NaN, 'one', null, 'two', 0, null, undefined, 'three', null, ]; const results = arr.filter(element => { return element; }); console.log(results); // ๐๏ธ [ 'one', 'two', 'three' ]
The filter()
method has to determine if the value of each element is truthy or
falsy as it only adds truthy values to the results
array.
You can imagine that each element gets passed to the Boolean()
constructor and
only truthy elements return true
and get included in the new array.
const arr = [null, NaN, 'one', null, 'two', 0, null, undefined, 'three', null]; const results = arr.filter(element => { return Boolean(element); }); console.log(results); // ๐๏ธ [ 'one', 'two', 'three' ]
If you have to remove all null
values from an array often, define a reusable
function.
function removeNull(array) { return array.filter(element => { return element !== null; }); } const arr = [ 'one', null, 'two', 0, null, undefined, 'three', null, ]; const result = removeNull(arr); console.log(result); // ๐๏ธ [ 'one', 'two', 0, undefined, 'three' ]
The removeNull()
function takes an array and removes all null
values from
the array.
Similarly, you can define a reusable function that removes all undefined
values from an array.
function removeUndefined(array) { return array.filter(element => { return element !== undefined; }); } const arr = ['a', undefined, 'b', undefined, 'c', undefined]; // ๐๏ธ [ 'a', 'b', 'c' ] console.log(removeUndefined(arr));
An alternative approach is to use the Array.forEach()
method.
This is a four-step process:
results
variable and set it to an empty array.forEach()
method to iterate over the array.null
.results
array.const arr = ['one', null, 'two', null, 'three', null]; const results = []; arr.forEach(element => { if (element !== null) { results.push(element); } }); console.log(results); // ๐๏ธ ['one', 'two', 'three']
The same approach can be used to remove all undefined
values from an array.
const arr = ['a', undefined, 'b', undefined, 'c', undefined]; const results = []; arr.forEach(element => { if (element !== undefined) { results.push(element); } }); console.log(results); // ๐๏ธ ['a', 'b', 'c']
The function we passed to the Array.forEach() method gets invoked with each element in the array.
On each iteration, we check if the current element is not equal to null
before
pushing it into the results
array.
The new array only contains the non-null elements of the original array.
If you have to remove the null
values from arrays often, define a reusable
function.
function removeNull(array) { const newArray = []; array.forEach(element => { if (element !== null) { newArray.push(element); } }); return newArray; } const arr = ['one', null, 'two', null, 'three', null]; const result = removeNull(arr); console.log(result); // ๐๏ธ [ 'one', 'two', 'three' ]
The function takes an array as a parameter and removes all null
values from
the array.
You can tweak the function slightly if you need to remove the undefined
values
from an array.
function removeUndefined(array) { const newArray = []; array.forEach(element => { if (element !== undefined) { newArray.push(element); } }); return newArray; } const arr = ['a', undefined, 'b', undefined, 'c', undefined]; const results = removeUndefined(arr); console.log(results); // ๐๏ธ [ 'a', 'b', 'c' ]
The function takes an array as a parameter and removes all undefined
values
from the array.
To remove all empty elements from an array:
filter()
method to iterate over the array.null
and undefined
.filter()
method will return an array containing only the non-empty
elements of the original array.const arr = [ 'one', null, 'two', undefined, 'three', null, undefined, ]; const results = arr.filter(element => { return element !== null && element !== undefined; }); console.log(results); // ๐๏ธ [ 'one', 'two', 'three' ]
The function we passed to the Array.filter()
method gets called with each
element in the array.
filter()
method adds the element to the results
array.In the example, we remove all empty, null
and undefined
elements from the
array.
Note that empty elements get removed automatically because the function never gets called with an empty element.
Notice that we used the Logical AND &&
operator which only returns true
if
both conditions return true
.
NaN
values. In this case, add additional conditions using the &&
(and) operator.const arr = ['one', '', 'two', , undefined, 0, 'three', null, NaN]; const results = arr.filter(element => { return ( element !== null && element !== undefined && element !== '' && !Number.isNaN(element) ); }); console.log(results); // ๐๏ธ [ 'one', 'two', 0, 'three' ]
We removed all empty, undefined
, null
, empty string or NaN
elements from
the array.
&&
operator, so all conditions have to be met for the element to get included in the results
array.You can use the logical AND &&
operator to chain as many conditions as
necessary.
You can also use a basic for
loop.
null
values from an array using a for
loopThis is a four-step process:
results
variable and initialize it to an empty array.for
loop to iterate over the original array.null
.results
array.const arr = ['one', 'two', null, undefined, 0, 'three', null, undefined]; // โ Remove null values from an array const results1 = []; for (let index = 0; index < arr.length; index++) { if (arr[index] !== null) { results1.push(arr[index]); } } // ๐๏ธ [ 'one', 'two', undefined, 0, 'three', undefined ] console.log(results1); // ----------------------------------------------- // โ Remove null and undefined values from an array const results2 = []; for (let index = 0; index < arr.length; index++) { if (arr[index] !== null && arr[index] !== undefined) { results2.push(arr[index]); } } // ๐๏ธ [ 'one', 'two', 0, 'three' ] console.log(results2);
We used a for
loop to iterate over the array.
On each iteration, we check if the current element is not equal to null
.
If the condition is met, we push the element into the results
array.
Which approach you pick is a matter of personal preference. I'd use the
Array.filter()
method as I find it quite direct and intuitive.
You can learn more about the related topics by checking out the following tutorials: