Saturday 20 October 2018

JavaScript: Set.prototype.clear(): Remove all elements from set

Set.prototype.clear()
‘clear’ method remove all the elements from 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);

console.log("\nClearning all the elements from set\n");
countries.clear();

printSetInfo(countries);


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

Clearning all the elements from set

***********************************
Number Of Elements : 0
***********************************



Previous                                                 Next                                                 Home

No comments:

Post a Comment