The set.size
property in JavaScript returns the number of elements in the stack. In other words, this property is used to get the current size of the set.
The following illustration shows the visual representation of the set.size
property:
In JavaScript, a set
is a particular instance of a list in which all inputs are unique.
Note: If there are duplicates, then the set will only keep the first instance. For example:
{'Tom','Alsvin','Eddie','Tom'}
will result in{'Tom','Alsvin','Eddie'}
.
set_name.size
// where the set_name is the name of the set.
This property does not require a parameter.
The set.size
property returns the number of elements in the set.
The code below shows how to use the set.size
property in JavaScript:
const set_1 = new Set(["Tom","Alsvin", "Eddie"]);//set containing valueconsole.log("The size of set_1: ",set_1.size);//empty setconst set_2 = new Set();console.log("The size of set_2: ",set_2.size);
Line 1: We create a set with three values {'Tom','Alsvin','Eddie'}
and name it set_1
.
Line 3: We check the size of set_1
, which contains elements, using set_1.size
.
Line 6: We create an empty set i.e. set_2
.
Line 7: We check the size of set_2
, which is empty, using set_2.size
.