What is the best way to count absolute array length in JavaScript?
<p>Just wondering, when you use <code>array.length</code> it gets the last index value and adds one.  What if you have an array, that is defined this way for some reason:</p>

<pre><code>    var myArray2 =[];
    myArray2[10]='x';
    myArray2[55]='x';
</code></pre>

<p>What is the absolute best way to get the true length of this Array? Something that would return 2 as the value.</p>

<p>I was thinking something like this, but not sure if there was already a method for this, or if there is a faster implementation.</p>

<pre><code>Array.prototype.trueLength= function(){
    for(var i = 0,ctr=0,len=myArray2.length;i&lt;len;i++){
        if(myArray2[i]!=undefined){
            ctr++;
        }
    }
    return ctr;        
}
console.log(myArray2.trueLength());
</code></pre>
