How to Clear a JavaScript Array JS Empty Array

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.

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

javascript reset array

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.

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.

Content and Platform Updates

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 https://bitcoin-mining.biz/ 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.

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

@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

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

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.

javascript reset array

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.

Empty an Array in JavaScript

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.

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.

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.

  • 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 you do, those references won’t be updated and those places will continue to use the old array.
  • Depending on where this array comes from this is safe to use because you’re not changing the values themselves.
  • @BekimBacaj No, splice mutates the original array and returns a new array.

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 Java 8 sum List of Integers with Stream 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.

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.

JavaScript Fundamentals

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.

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.