Empty an Array in JavaScript

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

There are various methods to empty an array in JavaScript. Based on the context of the application and the preference of the user, any of these can be used. For the purpose of this tutorial, we will consider the example of the array 'n' of length 5 with the values [9,5,4,3,7] in it. Our aim is to clear/empty this array.

Assigning It to a New Empty Array in JavaScript

Assigning an array to a new empty array in JavaScript is the simplest and fastest technique for emptying arrays.

In the above code, we simply assign array n to a new empty array. This works like a charm only if there are no references in the code to the original array.

However, in case there are any, those references won't be updated, i.e., they will continue to use the original array, which would further cause discrepancies in the code. To understand this shortcoming of this method better, let's see the following example:

Output:

In the above example, we can see that x is used to reference array n. When n is assigned to the empty array, the change is not reflected in n, thus causing the console to log the original array values. Use this JavaScript Formatter to format your code.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Setting Its Length to Zero

The length property of an array in JavaScript helps us to set the length of an array. If set to 0, all the elements of the array are deleted. The following code shows how it would be used:

When the length is set to 0, the size of the array reduces to 0, but this doesn't free up the objects in the array, i.e., the other values stay in the same locations but are not accessible when accessing the array. This can cause the code to have memory residuals. To clean them up, these values would need to be explicitly removed.

Using splice() Method

The splice() method is used to remove a number of elements of the array in JavaScript specified as parameters in the method. In the splice() method in JavaScript, the first argument contains the index of the array from where the removal should start, and the second argument contains the number of elements to be removed.

In the above code, the splice() method emptied the array as all the elements were removed.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Using pop() Method

Using the pop() method, each element in the array is popped one by one in a while loop till the array is emptied. This can be seen below:

Although the method seems quite easy, it takes the highest amount of time for execution since each element of the array is popped one at a time. Hence due to its slow performance, its use is not much preferred. Use this JavaScript Formatter to format your code.

Summary of Usage of the Various Methods

MethodWhen to Use
Assigning it to a new empty arrayWhen there are no references to the original array, and we want fast execution.
Setting its length to zeroWhen we are planning to clear the objects separately or if memory implications don't matter.
Using the splice() methodFor moderate performance and allows you to remove any number of elements from the array.
Using pop() methodWhen we want a trivial method and time is not our concern.

Take the first step towards becoming a JavaScript expert. Enroll in our Free JavaScript course and learn from industry experts.

Conclusion

  • There are various methods to empty an array in JavaScript.
  • Assigning the array to an empty array is the quickest method of emptying an array in JavaScript.
  • In JavaScript, length is a property that, when set to 0, clears the array.
  • splice() method can be used to delete 1 or more elements from the array in JavaScript.
  • pop() method deleted the array elements continuously to empty the array in JavaScript, thus taking more time to execute.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more