Return to Kroll Web Design home page

JavaScript Tutorials

« Return

length property (arrays)

To determine the length of an array, that is, to determine how many elements the array currently contains, look at the "length" property of the array.

Code:

<script type="text/javascript">
//<![CDATA[
var colors = new Array("red", "orange", "yellow", "green", "blue", "indigo", "violet");

document.write("colors.length : ", colors.length, "<br><br>");

for (i=0; i<colors.length; i++)
{
  document.write("colors[", i, "] : ", colors[i], "<br>");
}

poppedElement = colors.pop();

document.write("<br>poppedElement : ", poppedElement, "<br><br>");

document.write("colors.length : ", colors.length, "<br><br>");

for (i=0; i<colors.length; i++)
{
  document.write("colors[", i, "] : ", colors[i], "<br>");
}
//]]>
</script>

Output:




©2010 Kroll Web Design    davidarthurkroll@verizon.net    781.910.3694