Set.prototype.size

set.size
Returns: number · Updated March 13, 2026 · Map and Set
set size count cardinality

The size property returns the number of unique elements in a Set. It provides a constant-time way to determine how many distinct values the Set contains.

Syntax

set.size

Description

The size property is a getter that returns an integer representing the cardinality — the count of unique elements stored in the Set. Unlike arrays, Sets automatically eliminate duplicates, so size always reflects the actual number of distinct values.

Key Characteristics

  • Read-only: size is a getter property, not a method. It cannot be set directly.
  • Constant-time: Accessing size is O(1), regardless of how many elements are in the Set.
  • Auto-updating: The value updates automatically when elements are added or removed.
  • Empty sets return 0: A Set with no elements has a size of 0.

Examples

Basic Usage: Check Element Count

const fruits = new Set(['apple', 'banana', 'apple', 'orange', 'banana']);

console.log(fruits.size);
// 3 (duplicates are ignored)

Empty Set Returns Zero

const emptySet = new Set();

console.log(emptySet.size);
// 0

// Checking size before processing prevents unnecessary operations
if (emptySet.size > 0) {
  console.log('Processing items...');
} else {
  console.log('No items to process');
}
// No items to process

Comparing with Array.length

Arrays and Sets handle duplicates differently:

// Array: length includes duplicates
const array = [1, 2, 2, 3, 3, 3];
console.log(array.length);
// 6

// Set: size counts unique elements only
const set = new Set([1, 2, 2, 3, 3, 3]);
console.log(set.size);
// 3

// Adding more duplicates has no effect on Set size
set.add(2);
set.add(3);
console.log(set.size);
// Still 3

Practical: Check Before Processing

const userPermissions = new Set();

// Add some permissions
userPermissions.add('read');
userPermissions.add('write');

function checkAccess(requiredPermission) {
  // Quick size check for logging
  console.log(`Checking access. Total permissions: ${userPermissions.size}`);
  
  if (userPermissions.size === 0) {
    return 'No permissions configured';
  }
  
  return userPermissions.has(requiredPermission) ? 'Granted' : 'Denied';
}

console.log(checkAccess('read'));
// Checking access. Total permissions: 2
// Granted

console.log(checkAccess('delete'));
// Checking access. Total permissions: 2
// Denied

Size vs Length

AspectSetsizeArraylength
TypeGetter propertyProperty (writable)
DuplicatesCounts unique onlyCounts all elements
SettableNoYes (arr.length = 5)
Always accurateYesYes

Common Patterns

Guard Clauses

function processSetData(dataSet) {
  // Early return for empty set
  if (dataSet.size === 0) {
    return [Global_Objects::eval];
  }
  
  // Process only if set has data
  return [...dataSet].map(x => x.toUpperCase());
}

Conditional Processing

const tags = new Set(['javascript', 'es6', 'javascript']);

// Only save if we have unique tags
if (tags.size > 0) {
  saveToDatabase([...tags]);
}

See Also