Categories
JavaScript Answers

How to remove undefined values from an array with JavaScript?

Spread the love

Sometimes, we want to remove undefined values from an array with JavaScript.

In this article, we’ll look at how to remove undefined values from an array with JavaScript.

How to remove undefined values from an array with JavaScript?

To remove undefined values from an array with JavaScript, we can use the array filter method.

For instance, we write

let data = [42, 21, undefined, 50, 40, undefined, 9];
data = data.filter(Number);

to call data.filter with the Number function to return a new array without the undefined values in data.

Then we assign the new array back to data.

Conclusion

To remove undefined values from an array with JavaScript, we can use the array filter method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *