To check if a Set is empty in TypeScript, you can use the size
property of the Set object.
In this example,
set
and initialize it with some elements, or an empty Set if there are no elements.size
property of the Set set
to determine the number of elements it contains.let set: Set<string> = new Set(); // Or new Set(['a', 'b', 'c']) for a non-empty Set
let isEmpty: boolean = set.size === 0;
if (isEmpty) {
console.log('The Set is empty');
} else {
console.log('The Set is not empty');
}
The Set is empty
In this example,
emptySet
.size
property of the Set emptySet
to determine the number of elements it contains.let emptySet: Set<string> = new Set();
let isEmpty: boolean = emptySet.size === 0;
if (isEmpty) {
console.log('The Set is empty');
} else {
console.log('The Set is not empty');
}
The Set is empty
In this tutorial, we learned How to check if a Set is Empty in TypeScript language with well detailed examples.