To create an empty set in TypeScript, you can use the Set
constructor with no arguments.
In this example,
Set
constructor which is a built-in object in TypeScript that allows you to create a set.set1
and assign it the result of calling the new Set()
constructor with no arguments.new Set()
constructor creates a new, empty set.set1
to the console to verify its contents.let set1: Set<string> = new Set();
console.log('Contents of set1:', set1);
Contents of set1: Set(0) {}
In this example,
Set
constructor to create an empty set by calling new Set()
.set2
and assign it the result of the new Set()
constructor with no arguments.set2
using the add
method. For example, we add the strings 'apple'
and 'banana'
.set2
to the console to verify its contents.let set2: Set<string> = new Set();
set2.add('apple');
set2.add('banana');
console.log('Contents of set2:', set2);
Contents of set2: Set(2) { 'apple', 'banana' }
In this tutorial, we learned How to create an Empty Set in TypeScript language with well detailed examples.