Matthew C.
—You want to empty an array in JavaScript. What are the different ways of doing this?
There are several ways to clear an array. We’ll look at four methods.
The fastest and most straightforward way of emptying an array is by reassigning it to an empty array:
let arr = [1, 2, 3, 4, 5]; arr = []; console.log(arr); // []
The array [1, 2, 3, 4, 5]
is marked for garbage collection to free up memory as there is no reference to it after arr
is reassigned. The array is not reachable anymore.
This method only works if the array is not a const
variable, which can’t be changed by reassignment. If you change the above arr
variable to a const
variable, you’ll get the following error message:
Assignment to constant variable.
If you use this method to empty an array, bear in mind that the empty array is newly created. If another variable or property references the array, that variable or property will still point to the original array, as can be seen in the code example below:
let arr = [1, 2, 3, 4, 5]; const arr2 = arr; arr = []; console.log(arr); // [] console.log(arr2); // [1, 2, 3, 4, 5]
In JavaScript, objects are assigned and copied by reference, not by value. The variable arr2
does not copy the value of arr
when it’s assigned to it. It stores the reference to the array, which is its address in memory.
length
property to 0You can set the length
property of an array to 0 to empty an array:
arr.length = 0;
The array length
property is readable and writable, so you can use it to get or set the length of an array.
splice()
MethodYou can use the splice()
method to change an array by modifying the array itself. You can use it to remove all elements in an array:
arr.splice(0, arr.length);
The first argument, the start
, is the index position at which to start changing the array. The second argument, the deleteCount
, indicates the number of elements to remove in the array, starting from the start
index position. The code above removes all elements in the array.
pop()
MethodYou can also loop through an array and remove each element using the pop()
or shift()
methods, which remove the last element of an array and the first element of an array, respectively.
while (arr.length > 0) { arr.pop(); }
This is the slowest of the methods described here.
If you’re looking to get a deeper understanding of how JavaScript application monitoring works, take a look at the following articles:
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.