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;
🌐
Programiz
programiz.com › javascript › examples › empty-array
JavaScript Program to Empty an Array
The second argument is the number ... 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 = emptyArray(array); console.log(result);...
🌐
Sentry
sentry.io › sentry answers › javascript › how do i empty an array in javascript?
How do I Empty an Array in JavaScript? | Sentry
If another variable or property ... 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 ...
🌐
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.
🌐
Medium
medium.com › @ryan_forrester_ › empty-arrays-in-javascript-how-to-guide-f8643da412c2
Empty Arrays in JavaScript (How to Guide) | by ryan | Medium
October 30, 2024 - There are several ways to create empty arrays in JavaScript. The simplest way to create an empty array is by using array literals.
🌐
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 function, isEmpty, concisely encapsulates the logic needed to determine if an array is empty.
🌐
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 of el...
🌐
EDUCBA
educba.com › home › software development › software development tutorials › javascript tutorial › javascript empty array
JavaScript Empty Array | 9 Useful Examples of JavaScript Empty Array
July 6, 2023 - We have an even simple method of clearing the array elements, which removes elements from the end of the array by setting the array’s length. We need to set the value of length less than the current array length. Given below are the examples of JavaScript Empty Array:
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
Find elsewhere
🌐
Altcademy
altcademy.com › blog › how-to-create-an-empty-array-in-javascript
How to create an empty array in JavaScript
August 24, 2023 - In JavaScript, we can create an array using square brackets []. Here's how you would typically create an array: ... In this example, myFriends is an array that holds three strings. But what if we want to create an array without any items? That's where empty arrays come into the picture.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › Empty
Empty statement - JavaScript | MDN
const array = [1, 2, 3]; // Assign all array values to 0 for (let i = 0; i < array.length; array[i++] = 0 /* empty statement */); console.log(array); // Expected output: Array [0, 0, 0] ... The empty statement is a semicolon (;) indicating that no statement will be executed, even if JavaScript ...
🌐
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 - By using the Array.isArray method it can help to give us extra confidence that we're interacting with an array. For example, let's take this JavaScript object that has a length property: ... If we try to check if this object is empty using the length property, the expression will return true ...
🌐
JavaScript Tutorial
javascripttutorial.net › home › 4 ways to empty an array in javascript
4 Ways to Empty an Array in JavaScript
January 31, 2020 - The third way to empty an array is to remove all of its elements using the splice() method as shown in the following example: ... In this solution, the splice() method removed all the elements of the a array and returned the removed elements as an array. The fourth way to empty an array is to remove each element of the array one by one using the while loop and pop() method: while(a.length > 0) { a.pop(); }Code language: JavaScript (javascript)
🌐
TutorialsPoint
tutorialspoint.com › in-javascript-how-to-empty-an-array
In Javascript how to empty an array
There are multiple ways to clear/empty an array in JavaScript. You need to use them based on the context. Let us look at each of them. Assume we have an array defined as − let arr = [1, 'test', {}, 123.43]; Substituting with a new array − arr = []
🌐
Delft Stack
delftstack.com › home › howto › javascript › javascript declare empty array
How to Declare Empty Array in JavaScript | Delft Stack
March 11, 2025 - One of the most common ways to ... and easy to understand. ... In this example, we create a variable named emptyArray and assign it an empty array using square brackets....
🌐
Quora
quora.com › How-do-you-declare-an-empty-array-in-JavaScript
How to declare an empty array in JavaScript - Quora
Answer (1 of 19): You have two main ways to go: simple declaration with square brackets. const myArray = [] Or instantiation of the Array Object using the constructor method: const myArray = new Array() The trendy kids favor the first way, nowadays, with the empty square brackets, but if you h...
🌐
Flexiple
flexiple.com › javascript › check-if-array-empty-javascript
How to check if an array is empty using Javascript? - Flexiple
The reason for this is that in javascript each window or frame has its own execution environment, thus having a different scope from each other. This means that they have different built-in objects (i.e. different global objects, different constructors, etc.). This may lead to unexpected results when using instanceof, for example, for scripts passing objects from one context to another via functions. Considering such cases, it is best to simply use Array.isArray, especially when creating a framework, library, or a plugin, where the environment in which it will be used is not known in advance.
🌐
Vultr Docs
docs.vultr.com › javascript › examples › empty-an-array
JavaScript Program to Empty an Array | Vultr Docs
December 19, 2024 - Set the array's length to 0. ... This code outputs an empty array.
🌐
Codedamn
codedamn.com › news › javascript
How to empty an array in JavaScript
November 25, 2022 - All you need to do is to set the length to zero. Let us see an example of how it is done. var array1 = ["code", "damn", 0.001, 21, 67] array1.length = 0; console.log(array1)Code language: JavaScript (javascript)
🌐
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....