Ways to clear an existing array A:

Method 1

(this was my original answer to the question)

A = [];

This code will set the variable A to a new empty array. This is perfect if you don't have references to the original array A anywhere else because this actually creates a brand new (empty) array. You should be careful with this method because if you have referenced this array from another variable or property, the original array will remain unchanged. Only use this if you only reference the array by its original variable A.

This is also the fastest solution.

This code sample shows the issue you can encounter when using this method:

var arr1 = ['a','b','c','d','e','f'];
var arr2 = arr1;  // Reference arr1 by another variable 
arr1 = [];
console.log(arr2); // Output ['a','b','c','d','e','f']

Method 2 (as suggested by Matthew Crumley)

A.length = 0

This will clear the existing array by setting its length to 0. It also works when using "strict mode" in ECMAScript 5 because the length property of an array is a read/write property.

Method 3 (as suggested by Anthony)

A.splice(0,A.length)

Using .splice() will work perfectly, but since the .splice() function will return an array with all the removed items, it will actually return a copy of the original array. Benchmarks suggest that this has no effect on performance whatsoever.

Method 4 (as suggested by tanguy_k)

while(A.length > 0) {
    A.pop();
}

This solution is not very succinct, and it is also the slowest solution, contrary to earlier benchmarks referenced in the original answer.

Performance

Of all the methods of clearing an existing array, methods 2 and 3 are very similar in performance and are a lot faster than method 4. See this benchmark.

As pointed out by Diadistis in their answer below, the original benchmarks that were used to determine the performance of the four methods described above were flawed. The original benchmark reused the cleared array so the second iteration was clearing an array that was already empty.

The following benchmark fixes this flaw: http://jsben.ch/#/hyj65. It clearly shows that methods #2 (length property) and #3 (splice) are the fastest (not counting method #1 which doesn't change the original array).


This has been a hot topic and the cause of a lot of controversy. There are actually many correct answers and because this answer has been marked as the accepted answer for a very long time, I will include all of the methods here.

Answer from Philippe Leybaert on Stack Overflow
Top answer
1 of 16
5711

Ways to clear an existing array A:

Method 1

(this was my original answer to the question)

A = [];

This code will set the variable A to a new empty array. This is perfect if you don't have references to the original array A anywhere else because this actually creates a brand new (empty) array. You should be careful with this method because if you have referenced this array from another variable or property, the original array will remain unchanged. Only use this if you only reference the array by its original variable A.

This is also the fastest solution.

This code sample shows the issue you can encounter when using this method:

var arr1 = ['a','b','c','d','e','f'];
var arr2 = arr1;  // Reference arr1 by another variable 
arr1 = [];
console.log(arr2); // Output ['a','b','c','d','e','f']

Method 2 (as suggested by Matthew Crumley)

A.length = 0

This will clear the existing array by setting its length to 0. It also works when using "strict mode" in ECMAScript 5 because the length property of an array is a read/write property.

Method 3 (as suggested by Anthony)

A.splice(0,A.length)

Using .splice() will work perfectly, but since the .splice() function will return an array with all the removed items, it will actually return a copy of the original array. Benchmarks suggest that this has no effect on performance whatsoever.

Method 4 (as suggested by tanguy_k)

while(A.length > 0) {
    A.pop();
}

This solution is not very succinct, and it is also the slowest solution, contrary to earlier benchmarks referenced in the original answer.

Performance

Of all the methods of clearing an existing array, methods 2 and 3 are very similar in performance and are a lot faster than method 4. See this benchmark.

As pointed out by Diadistis in their answer below, the original benchmarks that were used to determine the performance of the four methods described above were flawed. The original benchmark reused the cleared array so the second iteration was clearing an array that was already empty.

The following benchmark fixes this flaw: http://jsben.ch/#/hyj65. It clearly shows that methods #2 (length property) and #3 (splice) are the fastest (not counting method #1 which doesn't change the original array).


This has been a hot topic and the cause of a lot of controversy. There are actually many correct answers and because this answer has been marked as the accepted answer for a very long time, I will include all of the methods here.

2 of 16
2755

If you need to keep the original array because you have other references to it that should be updated too, you can clear it without creating a new array by setting its length to zero:

A.length = 0;
🌐
Sentry
sentry.io › sentry answers › javascript › how do i empty an array in javascript?
How do I Empty an Array in JavaScript? | Sentry
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.
🌐
Ash Allen Design
ashallendesign.co.uk › blog › how-to-check-if-an-array-is-empty-in-javascript
How to Check If an Array Is Empty in JavaScript
January 8, 2024 - If the array is empty, the expression will return true like so: ... There are some caveats to using this approach, and we'll cover them further down in this article. A similar approach to the previous one is to use the length property with the ! operator. The ! operator is the logical NOT operator, and since in JavaScript 0 is a falsy value, we can use the !
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-empty-an-array-in-javascript
How to Empty an Array in JavaScript? - GeeksforGeeks
July 11, 2025 - // Given array let a = [ 10, 20, 30, 40, 50 ]; // Assign new array to make it empty a = []; // Display the updated array console.log("Updated Array: ", a) ... The JavaScript array.length property is used to set or return the length of the array.
🌐
freeCodeCamp
freecodecamp.org › news › check-if-javascript-array-is-empty-or-not-with-length
How to Check if a JavaScript Array is Empty or Not with .length
October 5, 2020 - By Madison Kanna When you're programming in JavaScript, you might need to know how to check whether an array is empty or not. To check if an array is empty or not, you can use the .length property. The length property sets or returns the number ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Errors › Reduce_of_empty_array_with_no_initial_value
TypeError: Reduce of empty array with no initial value - JavaScript | MDN
Another way would be to handle the empty case, either before calling reduce, or in the callback after adding an unexpected dummy initial value. ... const names = document.getElementsByClassName("names"); let nameList1 = ""; if (names.length >= 1) { nameList1 = Array.prototype.reduce.call( names, ...
🌐
CoreUI
coreui.io › blog › how-to-check-if-an-array-is-empty-in-javascript
How to check if an array is empty in JavaScript? · CoreUI
February 7, 2024 - This operation is crucial in various scenarios, such as enabling or disabling buttons based on user input or ensuring data integrity before processing. In this article, part of the “how to” series, we delve into the intricacies of checking for empty arrays in JavaScript.
🌐
Reddit
reddit.com › r/javascript › how to clear an array in javascript
r/javascript on Reddit: How to Clear an Array in JavaScript
August 2, 2023 - Because method 1 is a footgun, it doesn't empty the array, it creates a new one.
Find elsewhere
🌐
JavaScript Tutorial
javascripttutorial.net › home › 4 ways to empty an array in javascript
4 Ways to Empty an Array in JavaScript
January 31, 2020 - let a = [1,2,3];Code language: JavaScript (javascript) The following shows you several methods to make an array empty.
🌐
Sololearn
sololearn.com › en › Discuss › 1638302 › why-is-empty-array-or-list-falsey-in-python-but-truthy-in-javascript
Why is [] (empty Array or list) falsey in Python but, truthy in JavaScript? | Sololearn: Learn to code for FREE!
I'm having a little confusion about "emptyness" of lists and Arrays When I do: console.log(!![]); // then, it logs "true" but, when I do: print(not not []) // it print "False" ??? But, why these two languages have different implementation of the same concept? Is there any logic behind their implementation or are Arrays different from lists? javascriptpythonlistsarraysimplementationlogical...
🌐
Flexiple
flexiple.com › javascript › check-if-array-empty-javascript
How to check if an array is empty using Javascript? - Flexiple
Discover how to easily check if an array is empty using JavaScript. Our concise guide provides step-by-step instructions for efficient array handling.
🌐
DEV Community
dev.to › sanchithasr › 3-ways-to-empty-an-javascript-array-1lfm
3 ways to empty an Array in JavaScript - DEV Community
December 22, 2020 - Whoah, I never knew Array#length was settable! Setting it almost seems like an antipattern, because you'd expect it to be a read-only property, but it also looks to be the cleanest way to empty an array in-place (the splice version is even less intuitive).
🌐
Sololearn
sololearn.com › en › Discuss › 1742669 › how-to-create-empty-array-in-javascript-and-insert-the-values-after
How to create empty array in JavaScript and insert the values after | Sololearn: Learn to code for FREE!
javascriptarrays · 31st Mar 2019, ... · 31st Mar 2019, 7:54 AM · VEDANG · + 2 · You can create an empty array as var arrayName= []; and then to insert value in array you have many choices as 1....
🌐
W3Schools
w3schools.com › js › js_arrays.asp
JavaScript Arrays
JavaScript has a built-in array constructor new Array(). But you can safely use [] instead. These two different statements both create a new empty array named points:
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › check-if-an-array-is-empty-or-not-in-javascript
Check if an array is empty or not in JavaScript - GeeksforGeeks
July 11, 2025 - let a = []; if (Array.isArray(a) && a.length === 0) { console.log("Empty"); } else { console.log("Not Empty"); }
🌐
Reddit
reddit.com › r/learnprogramming › how do you remove an empty array within an array? js
r/learnprogramming on Reddit: How do you remove an empty array within an array? JS
November 19, 2022 -

I have some testing criteria and I've hit everything EXCEPT I can't filter out a fucking empty array within the array.

Array = [7, 10, 'hi', 'seven', null, [] ];

I've filtered everything and able to return only the even length strings but I can't get rid of the empty []

Searching Google keeps coming up short also with how to do it but I'm sure it's there, I just can't find it!

This is in JavaScript, thanks.

🌐
Altcademy
altcademy.com › blog › how-to-create-an-empty-array-in-javascript
How to create an empty array in JavaScript
August 24, 2023 - To create an empty array in JavaScript, you can use the following syntax: ... In this example, emptyArray is an array with no items. It's as simple as that! But why would you want to create an empty array?
🌐
freeCodeCamp
freecodecamp.org › news › how-to-clear-a-javascript-array-js-empty-array
How to Clear a JavaScript Array – JS Empty Array
June 27, 2022 - With a length of 0, every value in the array gets removed, and the array becomes empty.
🌐
Just Academy
justacademy.co › blog-detail › how-to-declare-empty-array-in-javascript
How to Declare Empty Array in JavaScript
In JavaScript, declaring an empty array is done simply by using empty square brackets `[]`. This is useful when you need to initialize a variable that will store a collection of elements but doesn't have any initial values yet.
🌐
Programiz
programiz.com › javascript › examples › empty-array
JavaScript Program to Empty an Array
The second argument is the number of elements that you want to remove from the index element. // program to empty an array function emptyArray(arr) { // setting array length to 0 arr.length = 0; return arr; } const array = [1, 2 ,3]; console.log(array); // call the function const result = ...