Skip to content

JavaScript Set size | Accessor property

  • by

Use the JavaScript Set size property to get the number of elements in a Set object. The size property returns the number of values in the Set object.

mySet.size

As opposed to the array’s length property, the size property is read-only and can’t be changed by the user.

JavaScript Set size

Simple example code.

<!DOCTYPE html>
<html>
<body>
  <script>
    const set1 = new Set([1,2,3,4]);

    console.log(set1);
    
    console.log("size", set1.size);

  </script>
</body>
</html>

Output:

JavaScript Set size property

More examples

const set1 = new Set();
const object1 = {};

set1.add(42);
set1.add('forty two');
set1.add('forty two');
set1.add(object1);

console.log(set1.size); // 3
const set = new Set(['a', 'b', 'c']);

console.log(set.size); // 👉️ 3

set.add('d');
set.add('e');

console.log(set.size); // 👉️ 5

Do comment if you have any doubts or suggestions on this Js set property topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *