Removing all elements of an array in JavaScript

Emptying an array is a common requirement in a JavaScript to compile new data. We can achieve this by different ways.

Removing all elements by making length zero.

We care read the number of elements in an array by using length property and we can set the length to zero. Here it is used to empty the array.
 <script type="text/javascript">
var scripts = new Array();
scripts[0] = "PHP";
scripts[1] = "ASP";
scripts[2] = "JavaScript";
scripts[3] = "HTML";
scripts[4] = "SQL";
document.write(scripts.join(","));
document.write("<br>--Now after emptying --<br>");
scripts.length=0; // Emptying all elements
document.write(scripts.join(","));
</script>

By re-declaring the array to new variable.

scripts = [];
or
var scripts = new Array();


plus2net.com




Post your comments , suggestion , error , requirements etc here .




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2019 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer