While the filter method filters through an array and returns a new array with the selected elements, the reduce methods contains a reducer function that does calculations on every element and returns a cumulative result at the end. The reduce method works like the filter method in that it iterates through all the elements of a given array. This will remove any current elements in the array, and we’ll end up with an empty array, as shown in the example below: let arr = Ĭonsole.log(`Array is '$) Using the reduce method įor our current purpose though, we’ll just be setting the length to 0. The ‘length’ property not only displays the current length of an array but can also be used to reduce or increase the length as we see fit.įor example, if we set the length of the array to 5, we’ve artificially increased its length, and the 2 remaining slots will be filled with undefined, like this. One of the easiest ways to reset an array is by setting its length to 0.
We’ll look at manipulating the length, assigning empty arrays to the original array, using various other pre-defined array methods, and looping through the array. In this tutorial, we’ll be looking at the different ways to empty an array in JavaScript.