JavaScript Get LengthSize of a Set

To get length or size of a Set in JavaScript, access size property of this Set. size is a read only property of Set object and returns an integer value representing the number of elements in this Set.

Syntax

The syntax to access size property on the Set set1 is

set1.size;

Examples

In the following example, we take a Set with two elements and find the size of this Set programmatically using size property.

index.html

<!DOCTYPE html>
<html lang="en">
<body>
    <pre id="output"></pre>
    <script>
        var set1 = new Set(['apple', 'banana']);
        var result = set1.size; //get size of set

        var displayOutput = 'Set size : ' + result;
        document.getElementById('output').innerHTML += displayOutput;
    </script>
</body>
</html>

The size of an empty Set is zero.

index.html

<!DOCTYPE html>
<html lang="en">
<body>
    <pre id="output"></pre>
    <script>
        var set1 = new Set();
        var result = set1.size; //get size of set

        var displayOutput = 'Set size : ' + result;
        document.getElementById('output').innerHTML += displayOutput;
    </script>
</body>
</html>

Conclusion

In this JavaScript Tutorial, we learned how to get the size of a Set in JavaScript using size property, with examples.