JavaScript Set size Property
Last Updated :
07 Jun, 2023
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
Here are some examples to illustrate the size property of the JavaScript Set object:
Example 1: In this example, we will see the use of the Javascript Set.size property.
Javascript
let myset = new Set();
myset.add(23);
myset.add(12);
console.log(myset);
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.
Javascript
let myset = new Set();
myset.set( 'Manchester' );
myset.add( 'London' );
myset.add( 'Norwich' );
console.log(set.size);
|
Output:
3
Supported Browsers:
- Google Chrome
- Firefox
- Opera
- Edge
- Safari
We have a complete list of Javascript Set methods, to check those please go through the Sets in Javascript article.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...