Skip to content
Related Articles
Open in App
Not now

Related Articles

How to empty an array in JavaScript ?

Improve Article
Save Article
  • Last Updated : 16 Dec, 2022
Improve Article
Save Article

There are many ways to empty an array using JavaScript, here in this article we will discuss and execute the top three ways to make an array empty.

  • Setting the array to a new array
  • By using the length property to set the array length to zero.
  • By using the pop() method we will be popping each element of the array.

Below examples will illustrate the above-mentioned ways to empty an array.

Example: Setting to a new Array means setting the array variable to a new array of size zero, then it will work perfectly. 

html




<body style="text-align:center;">
  
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
  
    <p id="up"></p>
  
    <button onclick="empty()">
        Click to Empty
    </button>
  
    <p id="down" style="color: green"></p>
  
    <!-- Script to set size of array to zero -->
    <script>
        var GFG_Array = [1, 2, 3, 4, 5];
        var up = document.getElementById("up");
        up.innerHTML = GFG_Array;
        var down = document.getElementById("down");
        down.innerHTML = "length of GFG_Array = "
                + GFG_Array.length;
        function empty() {
            GFG_Array = [];
            down = document.getElementById("down");
            down.innerHTML = "length of GFG_Array = "
                    + GFG_Array.length;
        }
    </script>
</body>

Output:

Empty an array using JavaScript by setting a new array

Example: By using the length property, we set the length of the array to zero which will make the array an empty array.

html




<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
  
    <p id="up"></p>
  
    <button onclick="empty()">
        Click to Empty
    </button>
  
    <p id="down" style="color: green"></p>
  
    <!-- Script to set the size of array to zero -->
    <script>
        var GFG_Array = [1, 2, 3, 4, 5];
        var up = document.getElementById("up");
        up.innerHTML = GFG_Array;
        var down = document.getElementById("down");
        down.innerHTML = "length of GFG_Array = "
                + GFG_Array.length;
                  
        function empty() {
            GFG_Array.length = 0;
            down = document.getElementById("down");
            down.innerHTML = "length of GFG_Array = "
                    + GFG_Array.length;
        }
    </script>
</body>

Output:

Empty an array using JavaScript length Property

Example: By using pop() method on each element of the array, we pops the array element continuously and get an empty array. But this method takes more time than others and is not much preferred. 

html




<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
  
    <p id="up"></p>
  
    <button onclick="empty()">
        Click to Empty
    </button>
  
    <p id="down" style="color: green"></p>
  
    <!-- Script to set the size of array to zero -->
    <script>
        var GFG_Array = [1, 2, 3, 4, 5];
        var up = document.getElementById("up");
        up.innerHTML = GFG_Array;
        var down = document.getElementById("down");
        down.innerHTML = "length of GFG_Array = "
                + GFG_Array.length;
                  
        function empty() {
            while(GFG_Array.length > 0) {
                GFG_Array.pop();
            }
            down = document.getElementById("down");
            down.innerHTML = "length of GFG_Array = "
                    + GFG_Array.length;
        }
    </script>
</body>

Output:

Empty an array using JavaScript pop() Method


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!