JavaScript - Count Array
July 4th, 2007I’m a PHP man myself however I’ve been playing around with JavaScript a lot lately since I’ve been creating the CRM for our web design company…
A cool little trick I learn’t today (I couldn’t find the answer easy enough in Google so I just figured it out myself) is how to count an array using JavaScript.
In PHP you’d simply write:
<?php
$myarray = array(’red’, ‘green’, ‘blue’);
echo count($myarray);
?>
This is how you do it in JavaScript:
<script language=”javascript” type=”text/javascript”>
var myarray = new Array(’red’, ‘green’, ‘blue’);
document.write(myarray.length);
</script>
Hopefully that helps anyone in the same predicament as I was ![]()