1// Example Array 2let n = [ 1, 2, 3, "a", "b", "c" ]; 3 4// Check if an array is empty in Javascript 5if (Array.isArray(n) && n.length === 0) { 6 console.log(“The array n is empty”); 7 8} else { 9 console.log(“The array n is not empty”); 10}; 11 12// Empty an array in Javascript by rewriting the length property 13n.length = 0; 14console.log(n) // output: []
There are occasions in Javascript in which we need to empty an array or check if the array is empty, so we can work with it. We have several ways to achieve this goal, we will see some of them next. If you want to know more about What is an array in Javascript you can read our guide on the 4Geeks Blog.
The best way to check if an array is empty is by accessing the length property of the array.
1let n = []; 2 3if ( Array.isArray(n) && n.length === 0 ) { 4 console.log(“The array n is empty”); 5} else { 6 console.log(“The array n is not empty”); 7};
In this example, we first verify that the array is of type array with the expression Array.isArray(n)
, because if it was another type of data, for example, an object we couldn't access the length property, then we verify that the array is empty with the expression n.length === 0
.
There are different ways in which we can empty an array in Javascript. Next, we will see some of them.
1let n = [ 1, 2, 3, 4, 5 ]; 2n.length = 0 3 4console.log( n ) // []
The most efficient way in terms of execution when emptying an array in Javascript is by accessing the length property of the array and assigning the value 0, this removes all the elements of the array leaving it completely empty.
Another way to empty an array in Javascript is by assigning it a new empty array.
1let n = [1, 2, 3]; 2const m = ["a", "b", "c"]; 3 4n = []; 5m = []; 6 7console.log(n) // (output) [] 8console.log(m) // (output) TypeError: Assignment to constant variable.
In this example, we assigned a new empty array []
to the n array, removing all its elements.
It is important to have in mind that if you use the const keyword to create the array, this syntax won't work for emptying an array because the constants can't be modified as in the array example m.
We can also use the splice() method to empty an array in Javascript.
1let n = [1, 2, 3]; 2let m = ["a", "b", "c"]; 3 4n.splice(0, n.length); 5m.splice(0, 1); 6 7console.log(n); // (output) [] 8console.log(m); // (output) ["b", "c"]
The splice method allows you to remove the elements of an array from a specific index, this method receives 2 numeric parameters, the first is the index from which you want to start removing elements, the second parameter is the number of elements you want to remove.
Finally, another option to empty an array is by using the pop()
method.
1let n = [1, 2, 3, 4, 5]; 2 3while (n.length > 0) { 4 n.pop(); 5}; 6 7console.log(n) // (output) [];
This is another way in which you can empty an array in Javascript. Using the pop method and the while()
loop removes all the elements of the array one by one.
In all the previous examples the elements are removed from the array, but not from the memory space, these elements are still in local memory, but since they don't have any reference the Javascript garbage collection ends up removing them. It is important to have this in mind because having elements in the memory without references could slow down the execution of your code.