How to empty an array in JavaScript?
There are many ways to empty an array in JavaScript some of them are discussed below:
Method 1: Setting to new Array: This method sets the array variable to new array of size zero, then it will work perfectly.
Example:
<!DOCTYPE html> <html> <head> <title> JavaScript to set array empty </title> </head> <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> </html> |
Output:
- Before clicking the button:

- After clicking the button:

Method 2: Using length property: This method sets the length of array to zero.
Example:
<!DOCTYPE html> <html> <head> <title> Create empty array </title> </head> <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> </html> |
Output:
- Before clicking the button:

- After clicking the button:

Method 3: Using pop method: This method pop the array element continuously and get the empty array. But this method takes more time than others and not much preferred.
Example:
<!DOCTYPE html> <html> <head> <title> Create empty array </title> </head> <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> </html> |
Output:
- Before clicking the button:

- After clicking the button:

Recommended Posts:
- Best way to initialize empty array in PHP
- Program to remove empty array elements in PHP
- RMS Value Of Array in JavaScript
- JavaScript | Array pop()
- JavaScript | Array some() function
- JavaScript | Array indexOf()
- JavaScript | Array from() Method
- JavaScript | Array.from() method
- JavaScript | Array toString()
- JavaScript | Array shift()
- JavaScript | Array unshift()
- JavaScript | Array forEach()
- JavaScript | Array push()
- JavaScript | Array copyWithin()
- JavaScript | Array reverse()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



