A set is a collection of values that are unique and without duplicates. Arrays are similar to sets. The only difference is that sets can only have unique elements. In TypeScript, the clear()
method is used to remove everything from the set.
set.clear()
set
: This is the set we want to clear all the values of.
This method returns an empty set.
// create some setslet names = new Set<string>(["Theodore", "David", "John", "Janme"])let evenNumbers = new Set<number>([2, 4, 6, 8, 10, 12])let booleanValues = new Set<boolean>([true, false])let countries = new Set<string>(["Nigeria", "Brazil", "Ghana", "Egypt", "Germany"])// logout all setsconsole.log("Before clearing sets")console.log(names)console.log(evenNumbers)console.log(booleanValues)console.log(countries)// clear the setsnames.clear()evenNumbers.clear()booleanValues.clear()countries.clear()// logout empty setsconsole.log("After clearing sets")console.log(names)console.log(evenNumbers)console.log(booleanValues)console.log(countries)
clear()
method to clear all the entries of the sets.