How to Clear Array in JavaScript

How to Clear Array in JavaScript? A variable called an array is used in JavaScript to store various components. It is frequently utilized when we wish to keep a list of elements and retrieve them using a single variable. In JavaScript, an array is a  variable that keeps several elements, unlike most languages where an array is a pointer to a collection of variables.

You can go over every item and combine results using JavaScript arrays. There are various techniques to add and eliminate array elements. However, clearing the entire array using the Array.prototype.clear method does not exist. In JavaScript, there are many ways to clear an array.

Different ways for JavaScript clear array

There are various techniques to clear an array in JavaScript.

1. Replace with a new empty array inplace of the existing array.
2. Set the length parameter for an array to 0.
3. Splice the entire array.

Clear JavaScript Array by Replacing with Empty Array

In Javascript, replacing an existing array with a brand-new empty array is the quickest way to clear or empty an array in Javascript. See the code below.

lang = ['C++', 'Python', 'JavaScript']
console.log('After Replacing with empty array:')
lang = []


[ ‘C++’, ‘Python’, ‘JavaScript’ ]
After Replacing with empty array:
[ ]

As you can see, we defined the lang array as having three elements and subsequently emptied it by replacing it with [ ]. In JavaScript, this is the quickest method for emptying an array.

The lang = [ ] assigns a variable a reference to a fresh array. Other references, however, are unaffected, which results in the retention of references to the data in a prior array, which causes memory leaks.

JavaScript Clear an Array by Setting the Length Parameter to 0

Modify the array size to 0 to clear the array. In this manner, the array will always be cleared and will not contain a single item. Every element in the array is removed, which does affect other entries.

Basically, if you use an array to remove the elements of an array and have two pointers to the same array (a = [‘cat’, ‘dog’, ‘parrot’]; a2 = a;). The pointers (a and a2) will now both refer to the same empty array if array.length = 0.

let pet = ['cat', 'dog', 'parrot'];
let set = ['cat', 'dog', 'parrot'];
let pet2 = pet;
let set2 = set;
pet = [];
set.length = 0;
console.log(pet, set, pet2, set2);


[ ] [ ] [ ‘cat’, ‘dog’, ‘parrot’ ] [ ]

JavaScript Clear Array using splice()

We can use the JavaScript array splice() tool to splice the entire array to clear it. Let us demonstrate it through an example:-

let lang = ['C++', 'Java', 'PHP'];
lang.splice(0, lang.length)


[ ]

We have used the splice() method by supplying the array size as the second argument and 0 as the first argument. This will clear the original array and provide a replica of the original elements, which may be helpful in some situations. However, it will also erase every item from the array.

This brings us to the end of this article where we learned how to clear an array in JavaScript using three different methods.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *