We use a set
as a data structure to store distinct values. Sets are prevalent across different programming languages. It is different from arrays because it makes sure that there are no duplicates in the values assigned to it.
new Set<type>()
type
: This represents the data type that the set must contain.A new set
with distinct values is returned.
// 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"])// log the sets to the consoleconsole.log(names)console.log(evenNumbers)console.log(booleanValues)console.log(countries)