# Get the length of a Map in JavaScript

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Dec 11, 2022, updated Jun 16, 2023. Topics: JavaScript.
Canonical URL: https://attacomsian.com/blog/javascript-map-get-length

> Learn how to use the size property to get the number of elements in a Map object in JavaScript.

The `size` property can be used to determine the length of a `Map` object. It returns the number of elements present in the `Map`.

```javascript
const map = new Map()

console.log(map.size) // 0

map.set('name', 'Atta')
map.set('age', 34)

console.log(map.size) // 2
```

The `size` property provides an integer value representing the number of entries in the `Map` object.

Unlike the `length` property of an array, which can be found [here](https://attacomsian.com/blog/javascript-arrays#the-length-property), the `size` property of a map is read-only and cannot be modified:

```javascript
const map = new Map([
  ['name', 'Atta'],
  ['age', 34]
])

console.log(map.size) // 2

map.size = 6

console.log(map.size) // 2 --> Remains the same
```

In contrast, the `length` property of an array is writable and can be assigned a new value to increase or decrease the size of the array:

```javascript
const numbers = [1, 2, 3, 4]

// Truncate to a 3-element array
numbers.length = 3

console.log(numbers)
// [1, 2, 3]

// Increase the size to 5
numbers.length = 5

console.log(numbers);
// [1, 2, 3, <2 empty items>]
```

To learn more about the `Map` object and how to create collections of key-value pairs in JavaScript, you can refer to [this article](https://attacomsian.com/blog/maps-javascript).
