How to check if a Map is Empty in JavaScript

avatar

Borislav Hadzhiev

2 min

banner

Photo from Unsplash

Check if a Map is Empty in JavaScript #

Use the size property to check if a Map is empty, e.g. map.size === 0. The size property returns the number of elements in the Map.

When accessed on an empty Map, the size property returns 0.

index.js
const map1 = new Map(); console.log(map1.size); // ๐Ÿ‘‰๏ธ 0 if (map1.size === 0) { // ๐Ÿ‘‡๏ธ this runs console.log('โœ… map is empty'); } else { console.log('โ›”๏ธ map is not empty'); }

We used the size property on the Map to check if it's empty.

The property returns the number of elements the Map stores.

index.js
const map2 = new Map(); map2.set('country', 'Chile'); console.log(map2.size); // ๐Ÿ‘‰๏ธ 1

The property is very similar to an array's length property, however, it is read-only and cannot be changed.

index.js
const map = new Map(); map.set('country', 'Chile'); console.log(map.size); // ๐Ÿ‘‰๏ธ 1 // โ›”๏ธ TypeError: Cannot set property size of #<Map> which has only a getter map.size = 5;

Even though we attempted to set the size property on the Map, we were unable to.

This behavior is different when working with arrays.

index.js
const arr = ['a', 'b', 'c']; console.log(arr.length); // ๐Ÿ‘‰๏ธ 3 arr.length = 1; console.log(arr.length); // ๐Ÿ‘‰๏ธ 1 console.log(arr); // ๐Ÿ‘‰๏ธ ['a']
As opposed to the Map's size property, we were able to change the array's length.

We set the array's length to 1 which automatically removed the last 2 elements from the array.

Map objects also have keys() and values() methods.

index.js
const map1 = new Map(); map1.set('name', 'bobby hadz'); map1.set('country', ['Chile', 'ab']); map1.set('age', 30); const keys = [...map1.keys()]; console.log(keys.length); // ๐Ÿ‘‰๏ธ 3 console.log(keys.length === 0); // ๐Ÿ‘‰๏ธ false const values = [...map1.values()]; console.log(values.length); // ๐Ÿ‘‰๏ธ 3 console.log(values.length === 0); // ๐Ÿ‘‰๏ธ false

The Map.keys method returns an iterator object that contains the keys of the Map.

The Map.values method returns an iterator object containing the values for each element in the Map.

We used the spread syntax (...) to convert the iterator objects to an array and accessed the length property on the arrays to check if the Map is empty.

However, using the size property on the Map object is more convenient and direct.

If you need to empty a Map object, use the Map.clear() method.

index.js
const map1 = new Map(); map1.set('name', 'bobby hadz'); map1.set('country', ['Chile', 'ab']); map1.set('age', 30); console.log(map1.size); // ๐Ÿ‘‰๏ธ 3 map1.clear(); console.log(map1.size); // ๐Ÿ‘‰๏ธ 0 console.log(map1); // ๐Ÿ‘‰๏ธ Map(0) {}

The Map.clear method removes all elements from a Map object.

If you have to check if a Map object is empty often, define a reusable function.

index.js
function mapIsEmpty(map) { return map.size === 0; } // ๐Ÿ‘‡๏ธ false console.log( mapIsEmpty( new Map([ ['name', 'bobby'], ['age', 30], ]), ), ); // ๐Ÿ‘‡๏ธ true console.log(mapIsEmpty(new Map()));

The mapIsEmpty() function takes a Map object as a parameter and returns true if it is empty and false otherwise.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2023 Borislav Hadzhiev