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 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.

javascript reset array

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.

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.

About This Article

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.

javascript reset array

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.

Watch Articles

Currently, I’m building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter What is CRM to see what I’m up to. This method can also work in case of “strict mode” in ECMAScript 5 because the length property is a read/write property.

javascript reset array

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.

So, inside your application’s/library’s IIFE, do function clear , then clear arrays with clear instead of arr.clear(). This is the fastest way to clear an array, but requires that you don’t have references to the original array elsewhere in your code. Setting the length to zero will empty the existing array.

How to Clear an Array in JavaScript (JS)

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.

She has more than 20 years of experience creating technical documentation and leading support teams at major web hosting and software companies. Nicole also holds an MFA in Creative Writing from Portland State University and teaches composition, fiction-writing, and zine-making at various institutions. If I reassign the value of wizards, the alsoWizards variable Embedded Systems Tutorial: What is, History & Characteristics still points to the original array that was assigned to it. But sometimes, you have an array that’s assigned by reference, and you want to keep them linked. The most common way to do this is to reassign the value of the wizards variable to an empty array ([]). We can use this method inside a loop and iterate until the length of the array is greater than zero.

  • Please don’t encourage modification of the native objects.
  • It is not currently accepting new answers or interactions.
  • Sometimes, you want to empty an array instead of adding one.
  • There might be an obscure javascript engine that already has clear() and expects it to behave a different way.
  • The splice() method adds or removes elements to or from an array.

Please use caution in deciding how to handle this. @daghan, it’s not at all clear what you’re trying to say. B holds a reference to the old array even after a is assigned a new one. This solution is not very succinct, and it is also the slowest solution, contrary to earlier benchmarks referenced in the original answer.

The simplest solution is to assign the array’s length to the value of 0. You can reassign a new value to a variable that initially has a non-empty array assigned to it. With a length of 0, every value in the array gets removed, and the array becomes empty. The length property of an array is readable and writeable. The following shows you several methods to make an array empty.

Advanced Functions

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 25 Web Developer Portfolio Examples from Top Developers 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.

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.

If you want to avoid mutating the arrays in place for immutability, you can overwrite the array with an empty array []. In this article, I’ll show and explain two ways to clear an array. Why developer experience is the key to better software, straight from the…

This should give you a more definite answer for the browser of your choice. A lot of answers here are old and I wouldn’t rely on them but rather test as in @tanguy_k’s answer above. This will return the copy of the original elements, which may be handy for some scenarios but will remove all items from the array and clean the original array. The fastest way to clear or empty an array in Javascript is by substituting an existing array with a new empty array. To clear anarray in Javascript, set an array length prop to 0.

It is “frowned upon” to modify javascript primitive functions like Array and String. You could possibly be overloading an already existing function and trash the object class. There might be an obscure javascript engine that already has clear() and expects it to behave a different way. @thefourtheye The whole point of not modifying global scope is because you won’t know if someone else’s code is already using the name.

This is perfect if you don’t have references to the original array A anywhere else because this actually creates a brand new 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. You can reset array values or clear the values very easily in PHP. 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. 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.