Several methods can be used to clear an array in JavaScript. The choice of the appropriate method to use depends heavily on your current use case. Factors like efficiency and readability of your code are more likely going to affect your final decision. Whether you are working on a frontend project, or a backend one using Node.js, this tutorial will teach you how to clear an array in JavaScript in the most effective way.
1. Using the Length Property
Among the array cleaning options in JavaScript, the simplest one is pointing the property length
of the array to zero.
let arr = [1, 2, 3, 4, 5]; arr.length = 0; console.log(arr); // Output: []
This way of erasing is especially useful for the cases when you need to keep the reference to the array unchanged, but the content is to be freed up.
2. Assigning a New Empty Array
Another way of clearing an array is to change the variable-reference to a new one. It does actually override the old one by assigning a new empty array to it.
let arr = [1, 2, 3, 4, 5]; arr = []; console.log(arr); // Output: []
This approach works fine if you are pretty sure that the array is not accessed (used) in other parts of your code.
3. Using the Splice Method
The splice()
method is a built-in function that allow you to remove elements from an array by giving the initial index of the slice. We can use it in a tricky way to clear the entire array as follow,
let arr = [1, 2, 3, 4, 5]; arr.splice(0, arr.length); console.log(arr); // Output: []
You should take into consideration that this approach doesn’t mutate the original array. So, the change will affect all its references in your code.
4. Using the Pop Method in a Loop
A very naïve way to clear an array is to use a loop with the pop()
method which allow you to delete the last element in the array.
let arr = [1, 2, 3, 4, 5]; while (arr.length > 0) { arr.pop(); } console.log(arr); // Output: []
Despite the fact that this method seem to be less elegant and too much wordy, you can use it when particular tasks need to be done on the elements during the removal.
5. Using the Shift Method in a Loop
Contrary to pop()
, the function shift()
is the one that allow you to remove elements from the beginning of the array.
let arr = [1, 2, 3, 4, 5]; while (arr.length > 0) { arr.shift(); } console.log(arr); // Output: []
Final Verdict
Different methods of cleaning an array in JavaScript offer different advantages and disadvantages. These latter have to be leveraged to pick the appropriate approach to achieve this task for your current use case. As a JavaScript developer, you should always thrive for maintainable and clean code.
If you are new to JavaScript and starting out, we have a free forever course for you that you can check here,
Learn JavaScript from Scratch for FREE
This course will teach you modern core JavaScript from the basic fundamentals to advanced topics. It is intended to make you learn JavaScript from scratch apart from its use for frontend/backend development or whatever. It aims mainly to give you an opportunity to build a solid foundation to become a better JavaScript developer no matterHappy coding 🙂