EN
JavaScript - check if string is empty
0 points
In this article, we would like to show you how to check if string is empty in JavaScript.
Quick solution:
xxxxxxxxxx
1
const myString = '';
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 = (string) => {
2
return string ? 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.