# Calculate the length of an associative array using JavaScript

• Last Updated : 07 Oct, 2021

Associative Array: In JavaScript, we have normal arrays in which an element is present at a particular index. Whereas Associative arrays are basically Objects in JavaScript where the index is replaced with user-defined keys. Basically, we can say that it stores Key-Value pairs.

Syntax:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

`let arr = { key1: 'value'1, key2: 'value2', key3: 'value3'}`

Here, arr is the associative array, and key1, key2, and key3 are its indexes, and value1, value2, and value3 are its elements.

Example:

`let arr = {"apple": 10, "grapes": 20};`

## Javascript

 ``

Output:

`Apple = 10 Banana = 40`

Length of an associative array: Like in a normal array, an associative array does not have a length property. So we will see other ways to calculate the length of associative arrays.

To calculate the length of an associative array, we will traverse the array element and count all the keys present in the array.

## Javascript

 ``

Output:

```{apple: 10, grapes: 20, guava: 30, banana: 40}
size = 4
{apple: 10, grapes: 20, guava: 30, banana: 40, fruits: 100}
Size = 5```

Using  the keys method: The keys() method returns an array containing all the keys present in the associative array. So, we can use the length property on this array to get the length of associative array.

## Javascript

 ``

Output:

```['apple', 'grapes', 'guava', 'banana']
Size = 4```

My Personal Notes arrow_drop_up