Check if a Map or a Set is Empty in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
4 min

banner

# Table of Contents

  1. Check if a Map is Empty in JavaScript
  2. Check if a Set is Empty in JavaScript

# 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'); }

check if map is empty

We used the Map.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.

# Check if a Set is Empty in JavaScript

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

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

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

check if set is empty

We used the Set.size property to check if a Set is empty.

The property returns an integer that represents how many elements the Set stores.
index.js
const set2 = new Set(['a', 'b']); console.log(set2.size); // ๐Ÿ‘‰๏ธ 2

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

index.js
const set1 = new Set(['bobby', 'hadz', 'com']); console.log(set1.size); // ๐Ÿ‘‰๏ธ 3 // โ›”๏ธ TypeError: Cannot set property size of #<Set> which has only a getter set1.size = 1;

We attempted to change the size property on the Set, however, we were unable to.

This is different when working with arrays, where the value of the length property can be set by the user.

index.js
const arr = ['bobby', 'hadz', 'com']; console.log(arr.length); // ๐Ÿ‘‰๏ธ 3 arr.length = 1; console.log(arr.length); // ๐Ÿ‘‰๏ธ 1 console.log(arr); // ๐Ÿ‘‰๏ธ [ 'bobby' ]

As opposed to a Set's size property we were able to set the array's length property and change the array's contents.

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

index.js
const set1 = new Set(['bobby', 'hadz', 'com']); console.log(set1.size); // ๐Ÿ‘‰๏ธ 3 set1.clear(); console.log(set1.size); // ๐Ÿ‘‰๏ธ 0 console.log(set1); // ๐Ÿ‘‰๏ธ Set(0) {}

empty a set object

The Set.clear() method removes all elements from a Set object.

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

index.js
function setIsEmpty(set) { return set.size === 0; } // ๐Ÿ‘‡๏ธ false console.log(setIsEmpty(new Set(['bobby', 'hadz']))); // ๐Ÿ‘‡๏ธ true console.log(setIsEmpty(new Set()));

The setIsEmpty function takes a Set object as a parameter and returns true if it is empty and false otherwise.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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 ยฉ 2024 Borislav Hadzhiev