Articles by FavTutor
  • AI News
  • Data Structures
  • Web Developement
  • AI Code GeneratorNEW
  • Student Help
  • Main Website
No Result
View All Result
FavTutor
  • AI News
  • Data Structures
  • Web Developement
  • AI Code GeneratorNEW
  • Student Help
  • Main Website
No Result
View All Result
Articles by FavTutor
No Result
View All Result
Home Web Developement

Empty an Array in JavaScript | 4 Methods (with code)

Komal Bhatia by Komal Bhatia
January 15, 2024
Reading Time: 5 mins read
Empty an Array in JavaScript
Follow us on Google News   Subscribe to our newsletter

In the coding world, we make use of arrays in almost every situation. But sometimes we need to clear an array, i.e. empty its contents. In this article, we will learn how to empty an array in JavaScript.

4 Methods to Empty an Array in JavaScript

Before moving to the main topic, we need to revise what an array is. Arrays are linear data structures that store homogenous data, i.e. same type of data. An array can contain all integers, all characters, or all strings as the elements.

Emptying an array means removing all elements from the array which makes it an array with a length of zero. The empty array will now have no values.

An empty/clear array can be necessary when you want to reset the array and remove all its elements. It can also be beneficial for memory management because there might be situations when some array holds a large amount of data and we no longer need it.

Let us look into various techniques to clear an array in JavaScript:

1) Re-Assignment of the Array

The simplest way to empty an array in JavaScript is to re-assign the original array to a new empty array that has no elements.

The following examples show how to do it:

//declaring an array
let arr=[1,2,3,4,5];

//reassigning the array to an empty array
arr=[]

//printing the array
console.log(arr);

Output:

[ ]

The output is an empty array, hence we have obtained the desired output. 

2) Modifying the Length of the Array

The length of the array specifies the number of elements that it contains. If the array has no elements, its length will be zero. So, in this technique, we update the length of the array to zero. Let us see an example:

//declaring an array
let arr=[1,2,3,4,5];

//printing the original length
console.log(arr.length)

//printing the original array
console.log(arr);

//modifying the length of an array
arr.length=0

//printing the modified length
console.log(arr.length);

//printing the modified array
console.log(arr);

Output:

5
[ 1, 2, 3, 4, 5 ]
0
[ ]

In this example, we have taken an array with five(5) elements. Hence, the original length is 5. Then, we update the length of the array to zero(0). This modifies the original array and its length. Array becomes empty(gets cleared) and the length of this empty array is zero. We have obtained the desired result.

3) Using the pop() Method

The pop() method is used to remove the last element from the array. So, we can use the pop() method in an array till its length becomes zero. This will clear the array. Here is how to do it:

//declaring an array
let arr=[1,2,3,4,5];

//using the pop() method
while(arr.length>0){
    //popping elements till length becomes zero
    arr.pop()
}

//printing the new length
console.log(arr.length);

//printing the new array
console.log(arr);

Output:

0
[ ]

We have used a while loop to pop the array elements till its length becomes zero. Hence, one by one the last element of the array gets removed. This clears the array. The array becomes empty and hence its length becomes zero. We have obtained the desired result. 

4) Using the Splice() Method

The splice() method in JavaScript removes elements from an array by specifying the starting index and the number of elements to be removed. We can specify the elements to be removed as the total number of elements in the array and can provide the starting index as zero(0). This will remove all the elements from the array, i.e. it will become empty. 

Check the example below:

//declaring an array
let arr=[1,2,3,4,5];

//using the splice method
arr.splice(0,arr.length);

//printing the new length
console.log(arr.length);

//printing the new array
console.log(arr);

Output:

0
[ ]

We have used the splice method to remove all the elements from the array. The array becomes empty and hence its length becomes zero. We have obtained the desired result.

Another interesting question asked in technical interviews is to reverse an array in JavaScript.

Conclusion

In this article, we have seen various techniques to clear an array in JavaScript. We can re-assign the array to an empty array, modify its length, use the pop() method till it becomes empty, or use the splice() method.

ShareTweetShareSendSend
Komal Bhatia

Komal Bhatia

I am a dedicated Computer Science student with a strong track record of academic excellence. I am a highly motivated individual with a passion for providing creative and innovative solutions to complex problems. I possess skills in the domain of C++ and web development and am always eager to contribute to the field of Software Development.

RelatedPosts

Javascript Animation Libraries

Top 10 JavaScript Animation Libraries in 2025

February 19, 2025
JavaScript Interview Questions

Top 40 JavaScript Interview Questions and Answers in 2024

April 1, 2025
Best React UI Component Libraries

10 Must-Know React UI Component Libraries for Web Devs 2024

May 7, 2024
Currying in JavaScript

Currying in JavaScript Explained (with Examples)

March 15, 2024
Javascript Format Currency

Javascript Program for Format Currency

April 8, 2025

About FavTutor

FavTutor is a trusted online tutoring service to connects students with expert tutors to provide guidance on Computer Science subjects like Java, Python, C, C++, SQL, Data Science, Statistics, etc.

Categories

  • AI News, Research & Latest Updates
  • Trending
  • Data Structures
  • Web Developement
  • Data Science

Important Subjects

  • Python Assignment Help
  • C++ Help
  • R Programming Help
  • Java Homework Help
  • Programming Help

Resources

  • About Us
  • Contact Us
  • Editorial Policy
  • Privacy Policy
  • Terms and Conditions

Website listed on Ecomswap. © Copyright 2025 All Rights Reserved.

No Result
View All Result
  • AI News
  • Data Structures
  • Web Developement
  • AI Code Generator
  • Student Help
  • Main Website

Website listed on Ecomswap. © Copyright 2025 All Rights Reserved.