Empty an Array in JavaScript
There are various methods to empty an array in JavaScript. Based on the context of the application and the preference of the user, any of these can be used. For the purpose of this tutorial, we will consider the example of the array 'n' of length 5 with the values [9,5,4,3,7] in it. Our aim is to clear/empty this array.
Assigning It to a New Empty Array in JavaScript
Assigning an array to a new empty array in JavaScript is the simplest and fastest technique for emptying arrays.
In the above code, we simply assign array n to a new empty array. This works like a charm only if there are no references in the code to the original array.
However, in case there are any, those references won't be updated, i.e., they will continue to use the original array, which would further cause discrepancies in the code. To understand this shortcoming of this method better, let's see the following example:
Output:
In the above example, we can see that x is used to reference array n. When n is assigned to the empty array, the change is not reflected in n, thus causing the console to log the original array values. Use this JavaScript Formatter to format your code.
Setting Its Length to Zero
The length property of an array in JavaScript helps us to set the length of an array. If set to 0, all the elements of the array are deleted. The following code shows how it would be used:
When the length is set to 0, the size of the array reduces to 0, but this doesn't free up the objects in the array, i.e., the other values stay in the same locations but are not accessible when accessing the array. This can cause the code to have memory residuals. To clean them up, these values would need to be explicitly removed.
Using splice() Method
The splice() method is used to remove a number of elements of the array in JavaScript specified as parameters in the method. In the splice() method in JavaScript, the first argument contains the index of the array from where the removal should start, and the second argument contains the number of elements to be removed.
In the above code, the splice() method emptied the array as all the elements were removed.
Using pop() Method
Using the pop() method, each element in the array is popped one by one in a while loop till the array is emptied. This can be seen below:
Although the method seems quite easy, it takes the highest amount of time for execution since each element of the array is popped one at a time. Hence due to its slow performance, its use is not much preferred. Use this JavaScript Formatter to format your code.
Summary of Usage of the Various Methods
Method | When to Use |
---|---|
Assigning it to a new empty array | When there are no references to the original array, and we want fast execution. |
Setting its length to zero | When we are planning to clear the objects separately or if memory implications don't matter. |
Using the splice() method | For moderate performance and allows you to remove any number of elements from the array. |
Using pop() method | When we want a trivial method and time is not our concern. |
Take the first step towards becoming a JavaScript expert. Enroll in our Free JavaScript course and learn from industry experts.
Conclusion
- There are various methods to empty an array in JavaScript.
- Assigning the array to an empty array is the quickest method of emptying an array in JavaScript.
- In JavaScript, length is a property that, when set to 0, clears the array.
- splice() method can be used to delete 1 or more elements from the array in JavaScript.
- pop() method deleted the array elements continuously to empty the array in JavaScript, thus taking more time to execute.