---
title: "Length of Javascript Object (ie. Associative Array)"  
description: "Length of Javascript Object (ie. Associative Array)"  
author: "Anonymous User"  
published: 2013-02-14  
updated: 2013-02-16  
canonical: https://www.mindstick.com/forum/606/length-of-javascript-object-ie-associative-array  
category: "javascript"  
tags: ["javascript"]  
reading_time: 1 minute  

---

# Length of Javascript Object (ie. Associative Array)

Hi!

If I have a [JavaScript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript) [associative array](https://www.mindstick.com/interview/434/explain-array-in-php) say:

var myArray = new Object();\
myArray["[firstname](https://www.mindstick.com/interview/33973/how-to-split-the-full-name-into-firstname-and-lastname-in-sql-server)"] = "Gareth";\
myArray["lastname"] = "Simpson";\
myArray["[age](https://www.mindstick.com/articles/23262/the-modern-age-these-technology-trends-can-make-your-life-easier)"] = 21;

\
Is there a built in or [accepted](https://yourviews.mindstick.com/view/80674/whatsapp-accepted-as-most-popular-app-worldwide) best [practice](https://yourviews.mindstick.com/story/1498/terrific-benefits-of-a-morning-meditation-practice) way to get the [length](https://www.mindstick.com/interview/1915/what-is-the-difference-between-char_length-and-length) of this array?

EDIT: JavaScript does not have associative [arrays](https://www.mindstick.com/articles/11955/arrays-and-its-limitations) -- it only has objects.

Please help me!

Thanks in [advance](https://www.mindstick.com/blog/33258/jee-mains-and-jee-advance-exams)!

## Replies

### Reply by AVADHESH PATEL

Hi Tanuj!

Get the length of JavaScript Object, try this way

```
Object.size = function(obj) {
    var size = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) size++;
    }
    return size;
};// Get the size of an object
var size = Object.size(myArray);
```


---

Original Source: https://www.mindstick.com/forum/606/length-of-javascript-object-ie-associative-array

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
