EN
TypeScript - check if string is empty
0 points
In this article, we would like to show you how to check if string is empty in TypeScript.
Quick solution:
xxxxxxxxxx
1
const myString: string = '';
2
3
if (myString === '') {
4
console.log('myString is empty');
5
} else {
6
console.log('myString is not empty');
7
}
In this example, we create a custom function that checks if the string is empty.
xxxxxxxxxx
1
const isEmpty = (text: string): boolean => {
2
return text ? false : true;
3
};
4
5
console.log(isEmpty('')); // true
6
console.log(isEmpty('sample text')); // false
Note:
This solution can be used to check if the string is
null
orundefined
too.