Contents
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. To use the splice() method, pass the first argument as 0 and the array length as the second parameter. We have defined an array dark with three elements and then emptied that array by substituting with . This is the fastest way to empty an array in Javascript. This article was co-authored by wikiHow staff writer, Nicole Levine, MFA. Nicole Levine is a Technology Writer and Editor for wikiHow.
He’s passionate about the hapi framework for Node.js and loves to build web apps and APIs. Creator of Futureflix and the “learn hapi” learning path. The value returned is the element with a new value. Because the new length is smaller than the original, the excess value is removed. The JavaScript Tutorial website helps you learn JavaScript programming from scratch quickly and effectively.
In most situations, reassigning the variable to an empty array is the better choice. It’s shorter and more explicit than adjusting the length property. Another way to clear an array is to set the array’s length property to 0. We can reset the value of the element of an array using the set method. It only requires the index position of the element to reset and the new value. This is the best way to reset/clear/empty an array in JavaScript.
Assigning it to a new empty array
In this example, first, the b variable references the array a. @BekimBacaj No, splice mutates the original array and returns a new array. That doesn’t necessarily mean they will shrink the storage for the actual array immediately. If I instead set the length of wizards to 0, the alsoWizards variable is also an empty array, because the array that wizards points to has not changed.
- Future Studio is helping 5,000+ users daily to solve Android and Node.js problems with 460+ written tutorials and videos.
- Benchmarks suggest that this has no effect on performance whatsoever.
- There are two methods to reset the array which are discussed further in this article.
- All the methods mentioned above are similar and can be used to clear the existing array, but the fastest ones are the second and third methods.
Please don’t encourage modification of the native objects. As per previous answer by kenshou.html, second method is faster. Lots of people think you shouldn’t modify native objects , and I’m inclined to agree.
@BekimBacaj i have updated my answer, I just assumed that length pops till length, but i corrected now that length just truncates or increases the size of an array. New Array() | [] Create an Array with new memory location by using Array constructor or array literal. So if arr has 5 items, it will splice 5 items from 0, which means nothing will remain in the array.
In Javascript how to empty an array
In this blog, we look at the different methods to clear/empty an array using JavaScript. There are multiple ways to empty an existing array in JavaScript. Emptying an array means removing all values from it.
A third way to clear an array is to use .splice(). Splicing won’t be as fast as the other two methods because it will return the removed elements as an array. This wikiHow teaches you how to empty a JavaScript array. There are three primary ways to clear an array—replacing the array with a new array, setting its property length to zero, and splicing the array. These three methods work similarly, although replacing the array with a new array shouldn’t be used if other code references the original array. The third method is using .splice(), which will work fine.
This code assigned the array a to a new empty array. It works perfectly if you do not have any references to the original array. You can easily create a function to do that for you, change the length or even add it to native Array as remove() function for reuse.
This is the only code that correctly empties the contents of a given JavaScript array. It is not currently accepting new answers or interactions. In a blog post, David Walsh mentions two approaches for emptying an array.

All the methods mentioned above are similar and can be used to clear the existing array, but the fastest ones are the second and third methods. Sometimes, you want to empty an array instead of adding one. However, there are multiple ways of clearing an array which we are going to discuss in this tutorial to make it much easier for you. Similar to the pop() method, we could use the shift() method too.
This solution is quite trivial and is the slowest one in terms of performance. If you look at the Array functions, there are many other ways to do this, but the most recommended one could be changing the length. Adding the percentage changes Hospital Apps arent much use without also noting your platform. On my machine pop is only very mildly quicker in Chrome 34 but actually slower than [] in latest Firefox. The answers that have no less that 2739 upvotes by now are misleading and incorrect.
This blog post explains the pros and cons of both approaches. In order to understand them, we first need to know about aliasing. Future Studio is helping 5,000+ users daily to solve Android and Node.js problems with 460+ written tutorials and videos. We’re on a mission to publish practical and helpful content every week. I’m Brad, and I’m nearing 20 years of experience with Linux. I’ve worked in just about every IT role there is before taking the leap into software development.
Substitution of an empty array
It isunambiguousand will affect both the variable containing the array and any variables referencing the array. This quick article will show you therightway to reset/clear/empty an array in the JavaScript programming language. Running the code above will set the arr to a new clean Offshore Software Development Services array. This is good if you don’t have references to the original array. The above example, removes all the elements of an array starting from zero essentially clearing the array. We continue with Flexiple’s tutorial series to explain the code and concept behind common use cases.

Using a new array will not affect any value that references your array. Depending on where this array comes from this is safe to use because you’re not changing the values themselves. Above, an array is declared and then emptied by setting the array’slengthproperty to0. Only if there are no references in the code to the original array. Another method you can encounter is setting the length of the array to 0. This method works as long as the array we want to clear has no references to it.
How to Clear a JavaScript Array – JS Empty Array
JavaScript arrays enable you to group values and iterate over each item. In addition, you can append and remove array elements in different ways. Unfortunately, there is no simple Array.prototype.clear method to clear the whole array. This will clear the existing array by setting its length to 0.
The length property is read/write property of an Array object. When the length property is set to zero, all elements of the array are automatically deleted. You will want to use the trash bin icon at the bottom to force a garbage collection after ‘clearing’ the array.
It’s not the count of values in the array, but the numeric index + 1. When you decrease it, the array will be truncated. Overall, the pop() & shift() methods require Best Affiliate Software 2022 Reviews a longer time & more number of lines of code as we remove elements one by one. The pop() method removes the last element of an array and returns that element.
Using this approach, you don’t leave work for the garbage collector, but possibly affect referenced arrays and reset them as well. All JavaScript runtimes (Browser, Node.js) are optimized for the operation to set the array length to zero. Setting it to 0 means, we are emptying the array. It removes each item in the array, which does hit other references.
