Blog

4 Ways to Empty an Array in JavaScript

214 Views0 Comment

The pop() method removes the last element of an array and returns that element. 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. 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.

A common approach to resetting an array is to re-declare it with an empty array – replacing the current value of the array with an empty one. 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. Similar to the pop() method, we could use the shift() method too. The only difference is that the shift() method removes the first element of the array instead of the last element like pop() method.

Clearing an array

It also works when using “strict mode” in ECMAScript 5 because the length property of an array is a read/write property. A third way to clear an array is to use .splice(). Cryptocurrency Exchange Script Bitcoin Exchange Script 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.

Unless otherwise noted, all code is free to use under the MIT License. I also very irregularly share non-codingthoughts. The value returned is the element with a new value. Because the new length is smaller than the original, the excess value is removed. In this article, I’ll show and explain two ways to clear an array.

There are multiple ways to empty an existing array in JavaScript. Emptying an array means removing all values from it. 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. @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.

So, inside your application’s/library’s IIFE, do function clear , then clear arrays with clear instead of arr.clear(). 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. Substituting an existing array with a new array. 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. If I reassign the value of wizards, the alsoWizards variable still points to the original array that was assigned to it.

It is not currently accepting new answers or interactions. This article was co-authored by wikiHow staff writer, Nicole Levine, MFA. Nicole Levine is a Technology Writer and Editor for wikiHow. She has more than 20 years of experience creating technical documentation and leading support teams at major web https://bitcoin-mining.biz/ 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. Let’s say you have an array of wizards, like this. I’m Brad, and I’m nearing 20 years of experience with Linux.

javascript reset 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. Also, this method is the fastest & easiest way to clear an array. This will remove all elements from the array and will actually clean the original array. @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.

How to Reset/Clear/Empty an Array in JavaScript

The above example, removes all the elements of an array starting from zero essentially clearing the array. When the length of an array is set to zero, all the array elements are automatically deleted. We continue with Flexiple’s tutorial series to explain the code and concept behind common use cases. In this blog, we look at the different methods to clear/empty an array using JavaScript. This is perfect if you don’t have any references from other places to the original arr. If you do, those references won’t be updated and those places will continue to use the old array.

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. The most common way to do this is to reassign the value of the wizards variable to an empty array ([]). This quick article will show you therightway to reset/clear/empty an array in the JavaScript programming language. This method can also work in case of “strict mode” in ECMAScript 5 because the length property is a read/write property.

Running the code above will set the arr to a new clean array. This is good if you don’t have references to the original array. 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.

javascript reset array

@LosManos Even in strict mode, length is a special property, but not read only, so it will still work. For precise memory measurements Chrome must be launched with –enable-precise-memory-info flag. We’re sorry but this website doesn’t work properly without JavaScript enabled.

Using the splice() method

New Array() | [] Create an Array with new memory location by using Array constructor or array literal. Lots of people think you shouldn’t modify native objects , and I’m inclined to agree. Please use caution in deciding how to handle this. This is the only code that correctly empties the contents of a given JavaScript array. This solution is not very succinct, and it is also the slowest solution, contrary to earlier benchmarks referenced in the original answer.

  • Splicing won’t be as fast as the other two methods because it will return the removed elements as an array.
  • This is the best way to reset/clear/empty an array in JavaScript.
  • In this article, I’ll show and explain two ways to clear an array.
  • The above example, removes all the elements of an array starting from zero essentially clearing the array.
  • Overall, the pop() & shift() methods require a longer time & more number of lines of code as we remove elements one by one.

Adding the percentage changes 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. @naomik But this is one of the basic functionalities, which should have been there by default. Find centralized, trusted content and collaborate around the technologies you use most.

How to Clear an Array by Modifying the Length Property

In order to understand them, we first need to know about aliasing. 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.

It won’t create much headache for you as a developer. Arrays are used to store several values in one variable. Neither the length of an array nor the types of its elements are fixed. Whenever you change the array, the length property automatically updates. It’s not the count of values in the array, but the numeric index + 1.

This solution is quite trivial and is the slowest one in terms of performance. Why developer experience is the key to better software, straight from the… I’ll update the original answer with the correct benchmark results. 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.

I’ve worked in just about every IT role there is before taking the leap into software development. 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 to see what I’m up to. If you want to avoid mutating the arrays in place for immutability, you can overwrite the array with an empty array []. Please don’t encourage modification of the native objects. As per previous answer by kenshou.html, second method is faster.

So if arr has 5 items, it will splice 5 items from 0, which means nothing will remain in the 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. @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. The difference in outputs is therefore expected. This has been a hot topic and the cause of a lot of controversy.

Leave your thought