How do I empty an array in JavaScript?



This tutorial teaches us to make an array empty in JavaScript. While programming with JavaScript, programmers need to make an array empty in many conditions. For example, coders are doing competitive programming with JavaScript. Suppose, to solve some problem, they need to create a new or an empty array while calling the function every time. Users can visualize this problem from the below example.

function child ( ){
   // create a new array or use a single array and make it empty every time function invokes
   }
   function parent ( ) {
      for ( int i=0; i< 10; i++ ) {
         Child ( );
      }
   }

In the above example, if the coder creates a new array every time, their code performance will decrease.

To overcome similar problems like the above, we have explained different approaches in this tutorial.

  • Assigning the new empty array

  • Making the array length zero

  • Using the pop() and shift() methods

  • Using the splice() method

Assigning the New Empty Array

In this approach, we will assign the new empty array ‘[ ]’ to the array to make the array empty. Assigning the empty array is the fastest and most popular approach to making an array empty. However, if users have taken reference to create the array, it will update the current array only, not the parent array from which they have taken reference.

Syntax

Users can follow the below syntax for this approach.

let array = ["welcome", "to", "the", "TutorialsPoint"];
array = [ ];

Example

In this example, we will look at the basic JavaScript to make an array empty by assigning it to an empty array.




   Make an array.


   

Make an array empty by assigning new empty array.

   

Initial array as follows:

   
   

Final array as follows:

   
   

In the above output, users can see that after assigning the empty array to the current array, it becomes empty.

Making the Array Length Zero

In JavaScript, every array has a length property that returns the number of elements inside the array. If we make an array length to 0, the array has no elements, and we can achieve our goal of making an array empty.

Syntax

Users can follow the below syntax to use the array length property and make it 0.

let array = ["welcome", "to", "the", "TutorialsPoint"];
array.length = 0;

Example

In this example, we will write JavaScript code to empty the array by making its length zero.



   Make an array empty.


   

Make an array empty in JavaScript by making array length 0.

   

Initial array:

   
   

Final array:

   
   

Using the pop() and shift() Methods

We can apply the pop() and shift() methods to the array in JavaScript. The Array.pop() method removes the single element from the last, and Array.shift() method removes the element from the first. Using this approach, we will remove elements from the array one by one until the array becomes empty.

Syntax

Users can follow the below syntax to use the Array.pop() and Array.shift() method.

let array = [ 10, 20, 30, 100, 200, 300 ];
array.pop( ) // removes element from the last.
array.shift( ) //removes element from the first.

Example

The below example demonstrates to make an array empty by using the array.pop() method. We will run the while loop and pop the single element from the array until the array becomes empty.



   Make an array empty.


   

Make an array empty in JavaScript by using the Array.pop() method.

   

Original Array of numbers:

   
   

Empty array:

   
   

Using the splice() Method

In this approach, we will use the splice() method to empty the array. The splice() method helps to remove the elements from the list or array in JavaScript. We will remove all elements from the array using the splice() method.

Syntax

Here is the syntax to use the splice() method and remove all elements from the array.

let chars = [ ‘T’, ‘u’, ‘t’, ‘r’, ‘I’, ‘a’, ‘l’, ‘s’ ];
let n = array.length;
chars.splice( start_index, n );

Parameters

The splice() method takes two parameters.

  • strart_index − It is the starting index, from where we want to start to remove the array elements.

  • n − It is the number of elements that we need to remove from the start index.

Example



   Make an array empty.


   

Make an array empty in JavaScript by using the Array.spilce() method.

   

Array of chars as follow:

   
   

Final array as follow:

   
   

Conclusion

Of the given four approaches, the first approach is the fastest, and the third approach is the slowest to making an array empty. As we need to loop through the whole array in the third approach, it is a very slow approach.

Updated on: 2022-07-20T08:40:09+05:30

504 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements