Saturday, 20 October 2018

JavaScript: Set.prototype.add(value): Add an element to set

Set.prototype.add(value)
add method appends an element to set.

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

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