🌐
OpenAI Developer Community
community.openai.com › api › bugs
Fine-tuned 4o errors using the responses API (with image input) - Bugs - OpenAI Developer Community
2 days ago - For the past 1 day, we’ve been getting incorrect responses from the responses API. The response keeps spamming an empty array: { "id": " ", "type": "message", "status": "completed", "content": [ { "type": "output_text", "annotations": [], "logprobs": [], "text": "" } ], "role": "assistant" ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array
Array - JavaScript | MDN
5 days ago - Methods that have special treatment for empty slots include the following: concat(), copyWithin(), every(), filter(), flat(), flatMap(), forEach(), indexOf(), lastIndexOf(), map(), reduce(), reduceRight(), reverse(), slice(), some(), sort(), and splice(). Iteration methods such as forEach don't ...
Discussions

!! vs ==0 when checking if array is empty
Why is this downvoted? It's literally the 'learnjavascript' sub. More on reddit.com
🌐 r/learnjavascript
63
94
June 30, 2024
How to Clear an Array in JavaScript
This raises at least 2 questions: in what situation would you want to "clear" an array? why would you even consider anything but method 1 (myVar = [];)? More on reddit.com
🌐 r/javascript
42
0
August 4, 2023
How do I empty an array in JavaScript? - Stack Overflow
Is there a way to empty an array and if so possibly with .remove()? For instance, A = [1,2,3,4]; How can I empty that? More on stackoverflow.com
🌐 stackoverflow.com
JavaScript WTF: Why does every() return true for empty arrays?
Because every member of the array meets the condition. It's logical More on reddit.com
🌐 r/javascript
40
0
September 5, 2024
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-empty-an-array-in-javascript
How to Empty an Array in JavaScript? - GeeksforGeeks
July 11, 2025 - We can directly assign an empty array literal to the variable, it will automatically remove all the elements and make the array empty.
🌐
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.
🌐
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. This guide promises ...
🌐
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)
🌐
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 ...
🌐
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 » ·
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › slice
Array.prototype.slice() - JavaScript | MDN
If end >= array.length or end is omitted or undefined, array.length is used, causing all elements until the end to be extracted. If end implies a position before or at the position that start implies, an empty array is returned.
🌐
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 !
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › Array
Array() constructor - JavaScript | MDN
Arrays can be created using a constructor with a single number parameter. An array is created with its length property set to that number, and the array elements are empty slots.
🌐
Code Like A Girl
code.likeagirl.io › create-an-array-of-empty-arrays-7ec77edea546
How to create an array of empty arrays | by Ai-Lyn Tang | Code Like A Girl
June 25, 2019 - Array.from makes an array out of this. The examples on the Mozilla developer documentation go through simpler use cases in my mind, so this also doesn’t quite make sense to me either. Namely why do I need Array.from at all? But I’ll accept it and move on for now.
🌐
Reddit
reddit.com › r/learnjavascript › !! vs ==0 when checking if array is empty
r/learnjavascript on Reddit: !! vs ==0 when checking if array is empty
June 30, 2024 -

I have an array in a function and I want the function to return true/false depending on if the array is empty (return true if not empty and vice versa)

I have narrowed down the condition to these 2 possible return statements. Which one is preferred?

return result.recordset.length == 0

return !!result.recordset.length
🌐
Reddit
reddit.com › r/javascript › how to clear an array in javascript
r/javascript on Reddit: How to Clear an Array in JavaScript
August 4, 2023 - Because method 1 is a footgun, it doesn't empty the array, it creates a new one.
🌐
Vultr Docs
docs.vultr.com › javascript › examples › empty-an-array
JavaScript Program to Empty an Array | Vultr Docs
December 19, 2024 - Discover the pros and cons of each technique to choose the best one for your specific use case. One of the simplest ways to empty an array is by setting its length property to zero.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › length
Array: length - JavaScript | MDN
When length is set to a bigger value than the current length, the array is extended by adding empty slots, not actual undefined values.
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;
🌐
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:
🌐
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...
🌐
Board Infinity
boardinfinity.com › blog › how-to-quickly-check-if-an-array-is-empty-or-not-in-javascript
Check if Array is Empty or Not-Javascript | Board Infinity
July 9, 2023 - The array.length property can be used to determine whether the array is empty. The array's element count is returned by this attribute.