Saturday 20 October 2018

JavaScript: Set.prototype.size: Get number of elements in set

Set has a property ‘size’, it specifies number of elements in the set.

HelloWorld.js
function printSetInfo(set){
  console.log("***********************************");
  console.log("Number Of Elements : " + set.size);
  
  for(var key of countries){
    console.log(key);
  }
  console.log("***********************************");
}

var countries =  new Set();

countries.add("India");
countries.add("Australia");
countries.add("Canada");
countries.add("Germany");

printSetInfo(countries);

Output
***********************************3
Number Of Elements : 4
India
Australia
Canada
Germany
***********************************


Previous                                                 Next                                                 Home

No comments:

Post a Comment