JavaScript Set Getting set length

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

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

const mySet = new Set([1, 2, 2, 3]);
mySet.add(4);
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


Got any JavaScript Question?