<p>I have a JavaScript object. Is there a built-in or accepted best practice way to get the length of this object?</p>
<pre><code>const myObject = new Object();
myObject[&quot;firstname&quot;] = &quot;Gareth&quot;;
myObject[&quot;lastname&quot;] = &quot;Simpson&quot;;
myObject[&quot;age&quot;] = 21;
</code></pre>

I'm not a JavaScript expert, but it looks like you would have to loop through the elements and count them since Object doesn't have a length method:
```
```
var element_count = 0;
for (e in myArray) {  if (myArray.hasOwnProperty(e)) element_count++; }
```
```
@palmsey: In fairness to the OP, the JavaScript documentation actually explicitly refer to using variables of type Object in this manner as "associative arrays".