JavaScript Set.size Property
The Set.size property in JavaScript returns the number of elements in a Set. If the Set is empty the size of the set will be returned as 0.
Syntax:
mySet.size
Here are some examples to illustrate the size property of JavaScript Set object:
Example 1:
Javascript
<script> // Create a new set using Set() constructor let myset = new Set(); // Append new elements to the // set using add() method myset.add(23); myset.add(12); // Print the modified set console.log(myset); // As the set has 2 elements, // it will return 2. console.log(myset.size); </script> |
Output:
Set(2) { 23, 12 } 2
Example 2:
Javascript
<script> // Creating a new set let myset = new Set(); // Adding new elements to the set myset.set( 'Manchester' ); myset.add( 'London' ); myset.add( 'Norwich' ); // As set has 3 elements, it will return 3 console.log(set.size); </script> |
Output:
3
Supported Browsers:
- Google Chrome: 38+
- Firefox: 19+
- Internet Explorer: 11+
- Opera: 25+
- Edge: 12+
- Safari: 08+
Please Login to comment...