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…
Scope of Article
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 Onion Architecture: Definition, Principles & Benefits 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 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.
- New Array() |  Create an Array with new memory location by using Array constructor or array literal.
- Include your email address to get a message when this question is answered.
- I also very irregularly share non-codingthoughts.
- Setting the length to zero will empty the existing array.
- The length property is read/write property of an Array object.
- You could possibly be overloading an already existing function and trash the object class.
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.
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.
Setting the length to 0
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.
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.
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 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.