Empty string, undefined, null, ...

To check for a truthy value:

if (strValue) {
    // strValue was non-empty string, true, 42, Infinity, [], ...
}

To check for a falsy value:

if (!strValue) {
    // strValue was empty string, false, 0, null, undefined, ...
}

Empty string (only!)

To check for exactly an empty string, compare for strict equality against "" using the === operator:

if (strValue === "") {
    // strValue was empty string
}

To check for not an empty string strictly, use the !== operator:

if (strValue !== "") {
    // strValue was not an empty string
}
Answer from Brian Dukes on Stack Overflow
Top answer
1 of 16
5116

Empty string, undefined, null, ...

To check for a truthy value:

if (strValue) {
    // strValue was non-empty string, true, 42, Infinity, [], ...
}

To check for a falsy value:

if (!strValue) {
    // strValue was empty string, false, 0, null, undefined, ...
}

Empty string (only!)

To check for exactly an empty string, compare for strict equality against "" using the === operator:

if (strValue === "") {
    // strValue was empty string
}

To check for not an empty string strictly, use the !== operator:

if (strValue !== "") {
    // strValue was not an empty string
}
2 of 16
1448

For checking if a variable is falsey or if it has length attribute equal to zero (which for a string, means it is empty), I use:

function isEmpty(str) {
    return (!str || str.length === 0 );
}

(Note that strings aren't the only variables with a length attribute, arrays have them as well, for example.)

Alternativaly, you can use the (not so) newly optional chaining and arrow functions to simplify:

const isEmpty = (str) => (!str?.length);

It will check the length, returning undefined in case of a nullish value, without throwing an error. In the case of an empty value, zero is falsy and the result is still valid.

For checking if a variable is falsey or if the string only contains whitespace or is empty, I use:

function isBlank(str) {
    return (!str || /^\s*$/.test(str));
}

If you want, you can monkey-patch the String prototype like this:

String.prototype.isEmpty = function() {
    // This doesn't work the same way as the isEmpty function used 
    // in the first example, it will return true for strings containing only whitespace
    return (this.length === 0 || !this.trim());
};
console.log("example".isEmpty());

Note that monkey-patching built-in types are controversial, as it can break code that depends on the existing structure of built-in types, for whatever reason.

🌐
W3Schools
w3schools.com › java › ref_string_isempty.asp
Java String isEmpty() Method
The isEmpty() method checks whether a string is empty or not. This method returns true if the string is empty (length() is 0), and false if not. public boolean isEmpty() None. ❮ String Methods · ★ +1 · Sign in to track progress · REMOVE ...
🌐
ThatSoftwareDude.com
thatsoftwaredude.com › content › 8774 › what-is-the-best-way-to-check-for-an-empty-string-in-javascript
The Best Way to Check for an Empty String in JavaScript - ThatSoftwareDude.com
August 23, 2024 - Discover the most efficient methods to check for an empty string in JavaScript. Learn best practices to ensure your code handles string validation effectively.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › system.string.isnullorempty
String.IsNullOrEmpty(String) Method (System) | Microsoft Learn
// String s2 is null or empty. // String s3 is null or empty. For more information about this API, see Supplemental API remarks for String.IsNullOrEmpty.
🌐
CoreUI
coreui.io › answers › how-to-check-if-a-string-is-empty-in-javascript
How to check if a string is empty in JavaScript · CoreUI
September 24, 2025 - For a more comprehensive check including null and undefined, use !text || text.length === 0. The strict comparison === 0 is preferred over == 0 to avoid type coercion issues and ensure precise empty string detection.
Find elsewhere
🌐
Programiz
programiz.com › java-programming › examples › string-empty-null
Java Program to Check if a String is Empty or Null
class Main { public static void main(String[] args) { // create null, empty, and regular strings String str1 = null; String str2 = ""; String str3 = " "; // check if str1 is null or empty System.out.println("str1 is " + isNullEmpty(str1)); // check if str2 is null or empty System.out.println("str2 is " + isNullEmpty(str2)); // check if str3 is null or empty System.out.println("str3 is " + isNullEmpty(str3)); } // method check if string is null or empty public static String isNullEmpty(String str) { // check if string is null if (str == null) { return "NULL"; } // check if string is empty else
🌐
freeCodeCamp
freecodecamp.org › news › check-if-string-is-empty-or-null-javascript
How to Check if a String is Empty or Null in JavaScript – JS Tutorial
November 7, 2024 - If the str variable is null, then we know that it's a null string. Otherwise, we know that the string is not empty or null. Another way to check for an empty string is to use the length property.
🌐
freeCodeCamp
freecodecamp.org › news › javascript-check-empty-string-checking-null-or-empty-in-js
JavaScript Check Empty String – Checking Null or Empty in JS
November 7, 2024 - In this first method, we will check for the length of the string by adding the length property. We'll check if the length is equal to 0. If it’s equal to zero, it means that the string is empty, as we can see below:
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-check-for-an-empty-string-in-javascript.php
How to Check for an Empty String in JavaScript
<script> if(str === ""){ // string is empty, do something } // Some test cases alert(2 === ""); // Outputs: flase alert(0 === "") // Outputs: false alert("" === "") // Outputs: true alert("Hello World!" === "") // Outputs: false alert(false === "") // Outputs: false alert(null === "") // Outputs: false alert(undefined === "") // Outputs: false </script> As you can see the values null, undefined, false returns false in the comparision, because they are special values. Please, check out the JavaScript data types chapter to learn more about it.
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-check-if-string-is-empty
How to check if a String is Empty in JavaScript | bobbyhadz
Use the `length` property on the string to check if it is empty. If the string's length is equal to `0`, then it's empty, otherwise, it isn't empty.
🌐
Cplusplus
cplusplus.com › reference › string › string › empty
std::string::empty
Returns whether the string is empty (i.e. whether its length is 0). This function does not modify the value of the string in any way. To clear the content of a string, see string::clear.
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › String › isEmpty
Java String isEmpty() - Check If Empty | Vultr Docs
December 17, 2024 - This code snippet checks if the username string is empty and provides feedback accordingly, crucial for avoiding processing of invalid or empty inputs. Handle input data where operations depend on whether strings are empty or not. Employ isEmpty() to conditionally process data only when strings are not empty.
🌐
Java67
java67.com › 2014 › 09 › right-way-to-check-if-string-is-empty.html
Right way to check if String is empty in Java with Example | Java67
An empty Java String is considered ... One of the most popular ways of checking whether String is empty or not is the String class' isEmpty() method, this looks perfect right, it's readable and returns boolean if String is empty ...
🌐
Reddit
reddit.com › r/learnprogramming › what is the best way to check for a blank string?
r/learnprogramming on Reddit: What is the best way to check for a blank string?
December 2, 2015 -

Perhaps this is trivial, but I got to wondering what is considered the "best" way to check for a blank string. I was specifically thinking of Javascript, although this could be applied to a number of languages. (Any C-style language) I thought up a couple solutions...

if (foo == "") ...

if (foo.length() == 0) ...

Not included in Javascript, but in languages that have it:

if (foo.isEmpty()) ...

Which of these is generally considered the most elegant/readable? Is it the single-purpose function, or a general-purpose function with a comparison? Or does it just not matter?

🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-check-empty-undefined-null-string-in-javascript
How to Check empty/undefined/null String in JavaScript? - GeeksforGeeks
July 11, 2025 - Using === operator we will check the string is empty or not. If empty then it will return "Empty String" and if the string is not empty it will return "Not Empty String". ... // Function to check string is empty or not function checking(str){ ...