To check if a Set is not empty in TypeScript, you can use the size property or the size() method.
In this example,
mySet
containing elements of a specific type.size
property to get the number of elements in the set.// Define non-empty set
const mySet: Set<string> = new Set(['apple', 'banana', 'orange']);
// Check if set is not empty using size property
if (mySet.size > 0) {
console.log('Set is not empty');
} else {
console.log('Set is empty');
}
Set is not empty
In this example,
mySet
containing elements of a specific type.size()
method to get the number of elements in the set.// Define non-empty set
const mySet: Set<string> = new Set(['apple', 'banana', 'orange']);
// Check if set is not empty using size() method
if (mySet.size() > 0) {
console.log('Set is not empty');
} else {
console.log('Set is empty');
}
Set is not empty
In this tutorial, we learned How to check if a Set is Not Empty in TypeScript language with well detailed examples.