Skip to main content

How to Check if a Map or Set is Empty in JavaScript

In JavaScript, Map and Set are two powerful data structures for handling collections of data. A common task is to check if a Map or Set is empty before you try to iterate over it or access its elements. Unlike arrays which use a .length property, these modern data structures use a different property for this purpose.

This guide will teach you the simple and standard method for checking if a Map or a Set is empty by using the built-in .size property.

The Core Property: .size

Both Map and Set objects have a built-in .size property. This property is a simple integer that returns the number of elements currently in the collection.

Therefore, the logic for checking if a Map or Set is empty is a simple comparison: myCollection.size === 0

If the .size is 0, the collection is empty.

Solution 1: Check if a Map is Empty

A Map is a collection of key-value pairs. The .size property tells you how many pairs are in the Map.

const myMap = new Map();

// Check the size of an empty Map
console.log(`Initial size: ${myMap.size}`); // Output: 0

if (myMap.size === 0) {
console.log('The Map is empty.');
} else {
console.log('The Map is not empty.');
}
// Output: The Map is empty.

// Add an element and check again
myMap.set('key1', 'value1');
console.log(`Size after adding an element: ${myMap.size}`); // Output: 1

Solution 2: Check if a Set is Empty

A Set is a collection of unique values. The .size property tells you how many unique values are in the Set.

const mySet = new Set();

// Check the size of an empty Set
console.log(`Initial size: ${mySet.size}`); // Output: 0

if (mySet.size === 0) {
console.log('The Set is empty.');
} else {
console.log('The Set is not empty.');
}
// Output: The Set is empty.

// Add an element and check again
mySet.add('value1');
console.log(`Size after adding an element: ${mySet.size}`); // Output: 1

The logic is identical to checking a Map.

.size vs. .length: An Important Distinction

It's a common mistake for beginners to try to use .length on a Map or Set.

  • .length is for Arrays and Strings.
  • .size is for Maps and Sets.

A key difference is that .size is a read-only property. You cannot change the number of elements in a Map or Set by assigning a new value to .size.

const myMap = new Map([['a', 1]]);
console.log(myMap.size); // Output: 1

myMap.size = 0; // This will have no effect.
console.log(myMap.size); // Output: 1

How to Empty a Map or Set

If you need to clear all the elements from a Map or Set, you should use the built-in .clear() method.

Clearing a Map

const myMap = new Map([['a', 1], ['b', 2]]);
console.log(`Size before clear: ${myMap.size}`); // Output: 2

myMap.clear();
console.log(`Size after clear: ${myMap.size}`); // Output: 0

Clearing a Set

const mySet = new Set(['a', 'b']);
console.log(`Size before clear: ${mySet.size}`); // Output: 2

mySet.clear();
console.log(`Size after clear: ${mySet.size}`); // Output: 0

Conclusion

Checking if a Map or Set is empty is a simple and direct operation thanks to the .size property.

The key takeaways are:

  1. Use the .size property to get the number of elements in a Map or Set.
  2. Check if the collection is empty with a simple comparison: myCollection.size === 0.
  3. Remember that .size is read-only. To remove all elements, use the .clear() method.