skip to main content

Empty a JavaScript Array

24 December, 2020

Posted in code

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