Prev Demo
JavaScript
>
Clear Array
Next Demo
Write code here
J
|
B
|
A
<!DOCTYPE html> <html> <head> <title>Javascript clear array</title> <script> function ClearArray() { var a = ["India", "Pakisthan", "Bangladesh", "China"]; alert("Original array: " + a); a = ["India", "Pakisthan", "Bangladesh", "China"]; a.length = 0; alert("1st output: " + a); // 2nd way a = ["India", "Pakisthan", "Bangladesh", "China"]; a = []; alert("2nd output: " + a); // 3rd way a = ["India", "Pakisthan", "Bangladesh", "China"]; a.splice(0, a.length); alert("3rd output: " + a); // 4th way a = ["India", "Pakisthan", "Bangladesh", "China"]; while(a.length) { a.pop(); } alert("4th output: " + a); } </script> </head> <body> <h1>Clear array in JavaSript</h1> <button onclick="ClearArray()">Clear Array</button> </body> </html>
Note: We DO NOT save your trial code in our database.
Output