6.7 Empty an Array
So now you know how to delete an element from an array. But what about clearing an array? How can we delete all the elements of that array. Well, there are a few different solutions. Here is the first solution. We can simply reassign this to a new array. Now, in this case, because we declared this array using const, we can't do that, so we have to change it, let. Now, let's save the numbers to the console, let's save the changes, so, now our array is empty. However, note that this old array is still in memory, so if there are no other references to this object, it will eventually be deleted by memory management. So this means that if we have say another variable called another, and that also points to the same object, that original array will not be retrieved. So here in line 6, when we reassign the Numbers variable, the numbers point to a new object in memory, but another still points to the old object. Let me show you this in detail. So here, instead of saving numbers, let's save another. Save the changes. So we can see that another still points to this array, to the original array, but if you look at numbers, they now point to a new object. To a new empty array. So this solution works if you have no other references to the original array. If you have multiple references to the original array, you have to use one of the other solutions. So I'll comment on that, let's look at solution 2, we can just set the lenghts property to 0, and that will truncate the array. It will remove all the elements. So, here, let's register another. Let's also save the original array. Save the changes. You can see that the original array is truncated, no matter how many references there are to the array. So this is solution number 2. The other solution is to use the splice method. So in the last video, you learned that with the splice method, you can go to a given position and delete one or more elements. So we can start from the first element, and delete all subsequent elements in this array. How many elements do we have in this array? Well, we do a numbers.lenghts Save changes, again, both arrays are empty. And finally the last solution is to use the pop method. So, solution 4. So you've learned that the pop method deletes the last element of this array. Now, we can loop this and keep calling this method, as long as we have an element in this array. So we do a while numbers.lenghts greater than 0. Save the changes, again, both arrays are empty. Now, this last solution is not something that I would recommend you do, because if you're dealing with a large array, let's say if you have a million objects in that array, there will be a performance cost. You essentially call this method pop a million times. but this is not the case with most applications, but in terms of clean coding, you can see that this approach is quite cumbersome, compare it to the first solution which is very simple, , the only problem is that it doesn't work if you have multiple references to that array. That's it for this video on how to empty an array in JavaScript, let's meet again for a very next video.