JavaScript Set size Property
The Set.size property in JavaScript returns the number of elements in a Set. In other words, we can say that this property is used to get the current size of the set. If the Set is empty the size of the set will be returned as 0.
Syntax:
mySet.size
Return Type:
- Number of elements in the set
Example 1: In this example, we will see the use of the Javascript Set.size property.
// 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);
Output:
Set(2) { 23, 12 }
2
Example 2: In this example, we will see the use of the Javascript Set.size property.
// Creating a new set
let mySet = new Set();
mySet.add('Manchester');
mySet.add('London');
mySet.add('Norwich');
console.log(mySet.size);
Output:
3
Supported Browsers:
- Google Chrome
- Firefox
- Opera
- Edge
- Safari