In the coding world, we make use of arrays in almost every situation. But sometimes we need to clear an array, i.e. empty its contents. In this article, we will learn how to empty an array in JavaScript.
4 Methods to Empty an Array in JavaScript
Before moving to the main topic, we need to revise what an array is. Arrays are linear data structures that store homogenous data, i.e. same type of data. An array can contain all integers, all characters, or all strings as the elements.
Emptying an array means removing all elements from the array which makes it an array with a length of zero. The empty array will now have no values.
An empty/clear array can be necessary when you want to reset the array and remove all its elements. It can also be beneficial for memory management because there might be situations when some array holds a large amount of data and we no longer need it.
Let us look into various techniques to clear an array in JavaScript:
1) Re-Assignment of the Array
The simplest way to empty an array in JavaScript is to re-assign the original array to a new empty array that has no elements.
The following examples show how to do it:
//declaring an array let arr=[1,2,3,4,5]; //reassigning the array to an empty array arr=[] //printing the array console.log(arr);
Output:
[ ]
The output is an empty array, hence we have obtained the desired output.
2) Modifying the Length of the Array
The length of the array specifies the number of elements that it contains. If the array has no elements, its length will be zero. So, in this technique, we update the length of the array to zero. Let us see an example:
//declaring an array let arr=[1,2,3,4,5]; //printing the original length console.log(arr.length) //printing the original array console.log(arr); //modifying the length of an array arr.length=0 //printing the modified length console.log(arr.length); //printing the modified array console.log(arr);
Output:
5
[ 1, 2, 3, 4, 5 ]
0
[ ]
In this example, we have taken an array with five(5) elements. Hence, the original length is 5. Then, we update the length of the array to zero(0). This modifies the original array and its length. Array becomes empty(gets cleared) and the length of this empty array is zero. We have obtained the desired result.
3) Using the pop() Method
The pop() method is used to remove the last element from the array. So, we can use the pop() method in an array till its length becomes zero. This will clear the array. Here is how to do it:
//declaring an array let arr=[1,2,3,4,5]; //using the pop() method while(arr.length>0){ //popping elements till length becomes zero arr.pop() } //printing the new length console.log(arr.length); //printing the new array console.log(arr);
Output:
0
[ ]
We have used a while loop to pop the array elements till its length becomes zero. Hence, one by one the last element of the array gets removed. This clears the array. The array becomes empty and hence its length becomes zero. We have obtained the desired result.
4) Using the Splice() Method
The splice() method in JavaScript removes elements from an array by specifying the starting index and the number of elements to be removed. We can specify the elements to be removed as the total number of elements in the array and can provide the starting index as zero(0). This will remove all the elements from the array, i.e. it will become empty.
Check the example below:
//declaring an array let arr=[1,2,3,4,5]; //using the splice method arr.splice(0,arr.length); //printing the new length console.log(arr.length); //printing the new array console.log(arr);
Output:
0
[ ]
We have used the splice method to remove all the elements from the array. The array becomes empty and hence its length becomes zero. We have obtained the desired result.
Another interesting question asked in technical interviews is to reverse an array in JavaScript.
Conclusion
In this article, we have seen various techniques to clear an array in JavaScript. We can re-assign the array to an empty array, modify its length, use the pop() method till it becomes empty, or use the splice() method.