JavaScript Set Getting set length


You can get the number of elements inside the set using the .size property

const mySet = new Set([1, 2, 2, 3]);
mySet.size; // 4

This property, unlike Array.prototype.length, is read-only, which means that you can't change it by assigning something to it:

mySet.size = 5;
mySet.size; // 4

In strict mode it even throws an error:

TypeError: Cannot set property size of #<Set> which has only a getter