var arr = new Array( "I", "learn", "JavaScript" ); // Three elements
alert( arr.length ); // 3
arr.push( "and", "it's", "really", "fun" ); // Add 4 new elements
alert( arr.length ); // 7
Note: the String object also has a length property, which counts the number of characters.
var arr = [ "I", "learn", "JavaScript" ]; // Length 3 array (three elements)
if( arr.length ) // True only if greater than zero
alert( "Array elements ready to be used!" );
else // false <-> length of zero
alert( "Cannot proceed - empty array!" );
Caveat: if the length is not working or says "undefined", your variable is not an array (or string).
for( var i=0; i<arr.length; ++i ) // Bad! Has to check it at each loop
for( var i=0, j=arr.length; i<j; ++i ) // Good! Only retrieves length once!
$arr = array( 'I', 'learn', 'PHP' );
echo count( $arr ); // 3