To check if a string is empty/undefined/null in JavaScript, you can use the “! operator” that returns true for empty strings, undefined values, null values, and other false values.
Example 1
function isEmpty(str) {
return !str;
}
const emptyString = '';
const nullValue = null;
const undefinedValue = undefined;
const nonEmptyString = 'Hello, world!';
console.log(isEmpty(emptyString));
console.log(isEmpty(nullValue));
console.log(isEmpty(undefinedValue));
console.log(isEmpty(nonEmptyString));
Output
true
true
true
false
This approach also considers other falsy values (e.g., 0, false, and NaN) to be “empty”, which may not be suitable for all use cases.
Example 2
You can use a more explicit conditional check to check specifically for empty strings, undefined values, or null values.
function isEmpty(str) {
return str === '' || str === null || str === undefined;
}
const emptyString = '';
const nullValue = null;
const undefinedValue = undefined;
const nonEmptyString = 'Hello, world!';
console.log(isEmpty(emptyString));
console.log(isEmpty(nullValue));
console.log(isEmpty(undefinedValue));
console.log(isEmpty(nonEmptyString));
Output
true
true
true
false
This function checks if the given value is an empty string, null, or undefined and returns true if any conditions are met.
How to check if a string is empty
To check if a string is empty(only empty), compare its “length property to 0” or directly compare it to an “empty string (‘ ‘)”.
Method 1: Comparing its length to 0
You can compare the string’s length to 0. If it is, it returns true otherwise, it is false.
const emptyString = "";
const nonEmptyString = "Harry, Potter!";
if (emptyString.length === 0) {
console.log("emptyString is empty");
} else {
console.log("emptyString is not empty");
}
if (nonEmptyString.length === 0) {
console.log("nonEmptyString is empty");
} else {
console.log("nonEmptyString is not empty");
}
Output
emptyString is empty
nonEmptyString is not empty
Method 2: Comparing a string to an empty string
You can compare an input string directly to an empty string.
const emptyString = "";
const nonEmptyString = "Harry, Potter!";
if (emptyString === "") {
console.log("emptyString is empty");
} else {
console.log("emptyString is not empty");
}
if (nonEmptyString === "") {
console.log("nonEmptyString is empty");
} else {
console.log("nonEmptyString is not empty");
}
Output
emptyString is empty
nonEmptyString is not empty
Both of these methods work great for checking if a string is empty.
Using the length property is often preferred since it’s more explicit, and it can also be used to check for strings containing only whitespace characters using the trim() method.
const stringWithSpaces = " ";
if (stringWithSpaces.trim().length === 0) {
console.log("stringWithSpaces is empty or contains only whitespace");
} else {
console.log("stringWithSpaces is not empty");
}
Output
stringWithSpaces is empty or contains only whitespace
In this code, the trim() method removes whitespace characters from the beginning and end of the string, and the length property is used to check if the resulting string is empty.

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.