How to Clear an Array in JavaScript

However, it will return a copy of the original array as the function returns an array with all the removed items. The splice method returns all the removed elements from an array. In this solution, the splice() method removed all the elements of the a array and returned the removed elements as an array. In the above code, the splice() method emptied the array as all the elements were removed.

  • It isunambiguousand will affect both the variable containing the array and any variables referencing the array.
  • He’s passionate about the hapi framework for Node.js and loves to build web apps and APIs.
  • But sometimes, you have an array that’s assigned by reference, and you want to keep them linked.
  • That doesn’t necessarily mean they will shrink the storage for the actual array immediately.
  • In most situations, reassigning the variable to an empty array is the better choice.

Please don’t encourage modification of the native objects. As per previous answer by kenshou.html, second Mobile app development Wikipedia method is faster. Lots of people think you shouldn’t modify native objects , and I’m inclined to agree.

Substitution of an empty 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. 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.

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

The Wrong Way – Re-declare the Variable

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

javascript reset array

Thanks to all authors for creating a page that has been read 14,688 times. Include your email address to get a message when this question is answered. Unless otherwise noted, all code is free to use under the MIT License. I also very irregularly share non-codingthoughts. Let’s say you have an array of wizards, like this. Find interesting tutorials and solutions for your problems.

Replace an array with an empty array

I tried it on most of browsers, like IE, Firefox, Chrome, it is creating new array. If you set length greater than 0 then it will create an array with undefined elements, i.e. it will just hold some memory locations. The original benchmark reused the cleared array so the second iteration was clearing an array that was already empty. Of all the methods of clearing an existing array, methods 2 and 3 are very similar in performance and are a lot faster than method 4. Benchmarks suggest that this has no effect on performance whatsoever.

javascript reset array

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

More Fundamentals Tutorials

Garbage collection is an optimized process in nowadays Node.js runtimes. It won’t create much headache for you as a developer. 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.

It also works when using “strict mode” in ECMAScript 5 because the length property of an array is a read/write property. Use this method if you only reference The Ultimate List of Interview Questions to Ask Remote Workers the array by its original variable arr. The original array will remain unchanged if you have referenced this array from another variable or property.

Why would you set the length instead of reassigning the variable?

The array.length is a built-in JavaScript property that sets or returns the number of items in the array. I also have an alsoWizards variable, and I set its value to the wizards array. This comes with the downside of creating a new array and leaving the old one for the garbage collector.

Fast solution, but this won’t free up the objects in this array and may have some memory implications. In order to clean objects in array from memory, they need to be explicitly removed. The splice() method adds or removes elements to or from an array. When we specify a.length, we are just resetting boundaries of the array and memory for rest array elements will be connected by garbage collector. This code will set the variable A to a new empty array.

The only difference is that the shift() method removes the first element of the array instead of the last element like pop() method. When the length of an array is set to zero, all the array elements are automatically deleted. A more verbose approach is to use the splice() method. This function will return a copy of the array before deleting the entries, which is helpful if you want to do a reassignment before clearing the array.

@LosManos Even in strict mode, length is a special property, Global Messaging Service Provider but not read only, so it will still work.

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.