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
🌐
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:
🌐
W3Schools
w3schools.com › jsref › jsref_array_new.asp
JavaScript new Array Method
❮ Previous JavaScript Array Reference Next ❯ · // Create an Array const cars = new Array(["Saab", "Volvo", "BMW"]); Try it Yourself » · More Examples Below ! The new Array() constructor creates an Array object. new Array(iterable) Array Tutorial · Array Const · Basic Array Methods · Array Search Methods · Array Sort Methods · Array Iteration Methods · Create an empty array and add values: // Create an Array const cars = new Array(); // Add Values to the Set cars.push("Saab"); cars.push("Volvo"); cars.push("BMW"); Try it Yourself » ·
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
You can set the length property of an array to 0 to empty an array: ... The array length property is readable and writable, so you can use it to get or set the length of an array.
🌐
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.
🌐
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 = []
🌐
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 - Next, let's use the logical "not" operator, along with our .length property, to test if the array is empty or not. If we had not used the "not" operator, arr.length would have returned 0. With the operator added, it will return true if its operand is false. Because arr.length is 0, or false, it returns true. Let's use this with an if statement, and print out a message if our array is empty.
🌐
JavaScript Tutorial
javascripttutorial.net › home › 4 ways to empty an array in javascript
4 Ways to Empty an Array in JavaScript
January 31, 2020 - When the length property is set to zero, all elements of the array are automatically deleted. The third way to empty an array is to remove all of its elements using the splice() method as shown in the following example:
Find elsewhere
🌐
W3Schools
w3schools.com › jsref › jsref_array[].asp
JavaScript Array []
❮ Previous JavaScript Array Reference Next ❯ · // Create an Array const cars = ["Saab", "Volvo", "BMW"]; Try it Yourself » · More Examples Below ! The [ ] constructor creates a new array object. [iterable] Array Tutorial · Array Const · Basic Array Methods · Array Search Methods · Array Sort Methods · Array Iteration Methods · Create an empty array and add values: // Create an Array const cars = []; // Add Values to the Set cars.push("Saab"); cars.push("Volvo"); cars.push("BMW"); Try it Yourself » ·
🌐
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 = ...
🌐
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...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › Empty
Empty statement - JavaScript | MDN
An empty statement is used to provide no statement, although the JavaScript syntax would expect one. 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]
🌐
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 !
🌐
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.
🌐
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 - This guide will provide you with a detailed understanding of how to create, check, and manipulate empty arrays in JavaScript, including practical examples.
🌐
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!
You can create an empty array as var arrayName= []; and then to insert value in array you have many choices as 1. Using for loop for(var i= 0; i<noOfElements; i++){ arrayName.push("value"); } 2.
🌐
Codecademy
codecademy.com › forum_questions › 5545872f51b887f7cd0002c0
What is the actual syntax for creating an empty array in js? | Codecademy
In **Introduction to Objects I**, i saw that in the lesson no. **26.Arrays of Objects** the empty array was declared as: `var family = new Array();` ...
🌐
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"); }
🌐
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.