Skip to main content

How to Get the Length (Size) of a Set in JavaScript

The Set object in JavaScript is a collection of unique values, and a fundamental operation is to determine how many items it contains. Unlike arrays which use the .length property, Set objects have a specific property for this purpose: .size.

This guide will teach you how to use the .size property to get the number of elements in a Set and explain its key differences from an array's .length property.

The Set.prototype.size property is the standard, built-in way to get the number of elements in a Set. It returns an integer representing the count of unique values.

For example, you have a Set and you want to know how many items it holds.

// Problem: How to get the number of elements in this Set?
const mySet = new Set(['apple', 'banana', 'cherry']);

This .size property is all you need as solution:

const mySet = new Set(['apple', 'banana', 'cherry']);

const setSize = mySet.size;

console.log(setSize); // Output: 3
note

If the Set is empty, .size will correctly return 0.

How the .size Property is Automatically Updated

The .size property is a "getter" that is automatically updated whenever you modify the Set. It provides a live, accurate count of the elements.

Solution:

const mySet = new Set();
console.log(mySet.size); // Output: 0

// Add elements
mySet.add('a');
mySet.add('b');
console.log(mySet.size); // Output: 2

// Remove an element
mySet.delete('a');
console.log(mySet.size); // Output: 1

// Clear all elements
mySet.clear();
console.log(mySet.size); // Output: 0

Set.size vs. Array.length: A Key Difference

While they serve a similar purpose, there is a key difference between a Set's .size and an Array's .length.

  • set.size is read-only. You cannot change the number of elements in a Set by setting its size property.
    const mySet = new Set(['a']);
    mySet.size = 5; // This has no effect
    console.log(mySet.size); // Output: 1
  • array.length is writable. You can change an array by setting its length. Setting it to a smaller number will truncate the array.
    const myArray = [1, 2, 3, 4, 5];
    myArray.length = 2;
    console.log(myArray); // Output: [1, 2]
note

This read-only nature of .size makes Sets more predictable and less prone to accidental mutation.

Why You Shouldn't Count Manually

While you could iterate over a Set with forEach and a counter to get its size, this is an unnecessary and non-idiomatic anti-pattern.

The Manual Method is not recommended!

const mySet = new Set(['a', 'b', 'c']);
let count = 0;

mySet.forEach(() => {
count++;
});

console.log(count); // Output: 3
note

You should never do this. The .size property is superior because it is:

  • Instantaneous: It's a direct property lookup (O(1)), whereas a loop requires iterating over every element (O(n)).
  • Simpler: It's a single property access.
  • More Readable: It clearly communicates your intent.

Conclusion

For getting the number of elements in a Set, the solution is simple and direct.

  • The set.size property is the only correct and recommended best practice.
  • It provides a read-only, live count of the elements in the Set.
  • Avoid manual counting methods (like using forEach), as the .size property is the most efficient and idiomatic solution.