To check if a string is empty in JavaScript, you can use the length
property of the String object.
In this example,
str1
with an empty string value ""
.length
property on str1
to check if it is empty.length
property returns the number of characters in the string, so if it is 0
, the string is empty.let str1 = "";
let isEmpty = str1.length === 0;
console.log("Is string empty: " + isEmpty);
Is string empty: true
In this example,
str2
with a non-empty string value "Hello World"
.length
property on str2
to check if it is empty.length
property returns the number of characters in the string, so if it is 0
, the string is empty.let str2 = "Hello World";
let isEmpty = str2.length === 0;
console.log("Is string empty: " + isEmpty);
Is string empty: false
In this tutorial, we learned How to Check if String is Empty in JavaScript language with well detailed examples.