Empty a JavaScript Array

Ahmed Awais shared this simple tip to empty a JavaScript Array.

arr.length = 0

So simple right? But why would you want to do this?

Perhaps your application uses an array to store a list of products, and when a user applies a filter facet, you could empty the array before populating it again with the new set of filtered products.

Check out the original tweet below for more discussion on the topic.

https://twitter.com/MrAhmadAwais/status/1256282166440452096

That's me!

I’m Ajay Karwal. I write about my experiences as a front-end developer. If you enjoy my content, please consider supporting what I do.

Become a PatronBuy me a coffee