JavaScript

Is There Any Method For Removing “undefined” Values From Array in JavaScript

Arrays are frequently utilized for storing data in JavaScript. These arrays may often contain undefined values, which might cause problems when working with the data. For instance, if you are trying to iterate through an array and perform a specified operation on each element, the existence of undefined values may result in errors or unexpected results.

This tutorial will explain the methods for removing “undefined” values from a JavaScript array.

Is There Any Method For Removing “undefined” Values From a JavaScript Array?

The following JavaScript predefined methods are used for removing undefined values from an array:

Method 1: Remove/Eliminate “undefined” Values From an Array Using “filter()” Method

To remove undefined values from an array, use the “filter()” method. It outputs a new array containing all elements that satisfy a particular test. It accepts a function as an argument that defines the condition against which each element is tested.

Syntax

Follow the given syntax for using the filter() method:

filter(callbackFnc(){

//condition

})

Example

Create an array that contains numbers with “undefined” values:

const array = [11, undefined, 20, 23, 8, undefined, 14, 15];

Call the filter() method where the callback function returns the elements that are not equal to “undefined”:

const filteredArray = array.filter(function (element) {

return element !== undefined;

});

Finally, display the resultant array on the console:

console.log(filteredArray);

As you can see, the output which indicates that the undefined values have been successfully removed:

Method 2: Remove/Eliminate “undefined” Values From an Array Using “reduce()” Method

Another way to remove undefined values from an array is to use the JavaScript “reduce()” method. It is used for iterating over an array and accumulating/collecting a single value based on the array’s elements. It accepts a callback function as an argument that is called on every element in the array, and the resultant value is used as the accumulator for the next iteration.

Syntax

The following syntax is utilized for reduce() method:

reduce(callbackFnc(){

//condition

}, initialValue)

Example

Invoke the reduce() method with the callback function an initial value that is an empty array ([ ]). The callback function will iterate the array and pick the elements that are not undefined and push them into the accumulator array. After each iteration, the callback function outputs the accumulator array:

const array = [11, undefined, 20, 23, 8, undefined, 14, 15];

const filteredArray = array.reduce((accum, element) => {

if (element !== undefined) {

accum.push(element);

}

return accum;

}, []);

Lastly, display the resultant accumulator array on the console:

console.log(filteredArray);

Output

We have compiled all the essential information for removing undefined values from an array in JavaScript.

Conclusion

To remove “undefined” values from an array, utilize the JavaScript predefined “filter()” method or the “reduce()” method. Both methods are good for removing undefined values while the filter() method is best because there is no need to push elements in another array. This tutorial explained the ways for removing “undefined” values from an array.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.