What is the set.delete() method in TypeScript?

Overview

A set is a data structure to store unique values that must not have duplicates. With the delete() method, we can delete an entry from a set.

Syntax

set.delete(value)
The syntax for the set.delete() method in TypeScript

Parameters

set: This is the set from which we want to delete an entry.

value: This is the value we want to delete from the set.

Return value

If the entry is found and successfully deleted, then true is returned. Otherwise, false is returned.

// create some sets
let 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"])
// check if they contain some values
console.log(names.delete("Theodore")) // true
console.log(evenNumbers.delete(5)) // false
console.log(booleanValues.delete(true)) // true
console.log(countries.delete("Tokyo")) // false
// log out the sets
console.log(names)
console.log(evenNumbers)
console.log(booleanValues)
console.log(countries)

Explanation

  • Lines 2–5: We create some sets.
  • Lines 8–11: We use the delete() method to delete some entries of the sets we created. Then, we log out the returned result to the console.
  • Lines 14–17: We log out the sets to the console after the deletion of some entries.

Free Resources