Set.prototype.size
Reports how many unique values the collection currently holds, as a read-only count.
mySet.size- Added in JavaScript ES2015
Return value
numberAn integer: how many unique elements the collection holds. It is an accessor property with a getter and no setter.
Errors
Assigning to it throws a TypeError in strict mode (including modules and class bodies) because there is no setter; in sloppy mode the assignment is silently ignored.
size is an accessor property, not a method — write s.size, never s.size(). It is the single most common typo on this type, because arrays use length and strings use length, so muscle memory produces both s.length (which is undefined) and s.size() (which throws "is not a function").
The count is maintained by the collection, so reading it is cheap — there is no counting pass. It reflects unique values only, which makes it the direct way to answer "how many distinct X are there?":
const distinct = new Set(rows.map((r) => r.country)).size;
No intermediate array, no sort.
Read-only
There is no setter. You cannot truncate a collection by assigning a smaller number the way you can with an array's length. In strict mode — which includes every ES module and class body — the assignment throws a TypeError; in sloppy mode it fails silently, which is worse. Use delete() or clear() to shrink it.
For emptiness checks, compare against zero: if (s.size === 0). A collection object is always truthy, so if (!s) never fires.
Example
When to use it
- Count how many distinct values a column takes across a dataset
- Detect duplicates by comparing a Set's size with the source array's length
- Check emptiness before rendering a filter chip row
- Confirm an insert was new by comparing the count before and after
Frequently asked questions
Why is Set size not a function?
It is defined as an accessor property with a getter, so you read it as s.size without parentheses. Writing s.size() throws "s.size is not a function" because the number it returns is not callable.
Does a Set have a length property?
No — s.length is undefined. Collections expose size while arrays and strings expose length, and the two are not interchangeable.
Can I shrink a Set by setting its size?
No. There is no setter, so the assignment throws a TypeError in strict mode and is silently ignored otherwise. Remove values with delete(), or empty the whole collection with clear().
More examples
Detecting duplicates without sorting
Comparing the deduped count with the original length is a one-line duplicate check that needs no sort and no nested loop. The last line counts distinct countries directly — 2, not 3.
function hasDuplicates(arr) {
return new Set(arr).size !== arr.length;
}
console.log(hasDuplicates([1, 2, 3]));
console.log(hasDuplicates([1, 2, 2]));
const rows = [
{ country: "NL" }, { country: "BE" }, { country: "NL" },
];
console.log(new Set(rows.map((r) => r.country)).size);Size as a change detector
Because add() always returns the Set, the size delta is what tells you whether the value was new. The output alternates new/duplicate/new and ends at a count of 2.
const seen = new Set();
function addAndReport(value) {
const before = seen.size;
seen.add(value);
return seen.size > before ? "new" : "duplicate";
}
console.log(addAndReport("x"));
console.log(addAndReport("x"));
console.log(addAndReport("y"));
console.log(seen.size);Emptiness checks
An empty collection is still an object, so truthiness tells you nothing — only size === 0 answers the question. The last line confirms the count returns to zero after a delete.
const empty = new Set();
console.log(empty.size === 0);
// The object itself is always truthy
console.log(Boolean(empty));
if (empty) console.log("this branch always runs");
empty.add(1);
empty.delete(1);
console.log(empty.size, empty.size === 0);
Discussion