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
5110

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
1446

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.

๐ŸŒ
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 - In this example, we're first using the trim method to remove any leading or trailing whitespace characters from the str variable, then checking whether the resulting string has zero length.
๐ŸŒ
Quora
quora.com โ€บ How-do-I-check-an-empty-undefined-null-string-in-JavaScript
How to check an empty/undefined/null string in JavaScript - Quora
Answer: If you want to check if the variable s contains an empty string or undefined or null, the easiest way would be to check if s is falsy using the not operator: [code]if (!s) { console.log("s is falsy"); } [/code]A problem is that this ...
๐ŸŒ
Medium
medium.com โ€บ @python-javascript-php-html-css โ€บ validating-empty-undefined-or-null-strings-in-javascript-fe483c3340ad
JavaScript Validation of Null, Undefined, and Empty Strings
August 24, 2024 - Yes, an empty string has a length of 0, while a string with spaces has a length corresponding to the number of spaces. Use string.trim().length === 0 to check for both. How do I check for both null and undefined in a single condition?
๐ŸŒ
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.
๐ŸŒ
Squash
squash.io โ€บ how-to-check-for-an-empty-string-in-javascript
How To Check For An Empty String In Javascript
September 5, 2023 - One way to check for an empty string in JavaScript is by using the length property of the string. The length property returns the number of characters in a string. If the length of a string is 0, it means the string is empty.
๐ŸŒ
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 - let myStr = " "; if (myStr.length === 0) { console.log("This is an empty string!"); }else{ console.log("This is NOT an empty string!"); } ... We can easily fix this error by first removing the white spaces using the trim() method before checking ...
Find elsewhere
๐ŸŒ
Attacomsian
attacomsian.com โ€บ blog โ€บ javascript-check-if-string-is-empty
How to check if a string is empty in JavaScript
October 23, 2022 - const str = ' ' if (str.trim().length === 0) { console.log(`String is empty โœ…`) } else { console.log(`String is not empty โŒ`) } // String is empty โœ… ยท The trim() method removes the leading and trailing spaces from a string.
๐ŸŒ
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 - To check for empty or whitespace-only strings, use text.trim().length === 0. 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 ...
๐ŸŒ
Roblog
robiul.dev โ€บ how-to-check-if-a-string-is-empty-in-javascript
How to Check if a String is Empty in JavaScript
June 13, 2023 - Next, we check if str is not null. The null value represents the intentional absence of any object value. In javascript, By checking whether the string is null or empty, we ensure that the variable has a valid value.
๐ŸŒ
Zipy
zipy.ai โ€บ blog โ€บ how-do-i-check-for-an-empty-undefined-null-string-in-javascript
how do i check for an empty undefined null string in javascript
April 12, 2024 - To ensure our application is robust and free from unexpected errors, it's important to handle cases where a string might be undefined or null. Considering JavaScript's type coercion and truthy/falsy evaluation, a more encompassing check can be performed to cover empty, undefined, and null strings in a single condition. let myString; if (!myString) { console.log("The string is empty, undefined, or null"); } else { console.log("The string has content"); }
๐ŸŒ
Tutorial Republic
tutorialrepublic.com โ€บ faq โ€บ how-to-check-for-an-empty-string-in-javascript.php
How to Check for an Empty String in JavaScript
You can use the strict equality operator (===) to check whether a string is empty or not. The comparsion str === "" will only return true if the data type of the value is string and it is not empty, otherwise return false as demonstrated in ...
๐ŸŒ
W3Resource
w3resource.com โ€บ javascript-exercises โ€บ javascript-string-exercise-2.php
JavaScript validation with regular expression: Check whether a string is blank or not - w3resource
July 17, 2025 - Inside the function, it checks if the length of the input string is equal to 0, indicating that the string is empty. If the length is 0, it returns 'true', indicating that the string is blank. If the length is not 0, it returns 'false', indicating that the string is not blank. The code then tests the function by calling it with an empty string '' and a non-empty string 'abc'. It prints the result of each function call using console.log. ... See the Pen JavaScript Check whether a string is blank or not - string-ex-2 by w3resource (@w3resource) on CodePen.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Operators โ€บ Nullish_coalescing
Nullish coalescing operator (??) - JavaScript | MDN
August 26, 2025 - The nullish coalescing (??) operator is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand. const foo = null ?? "default string"; console.log(foo); // Expected output: "default string" const baz = 0 ?? 42; console.log(baz); // Expected output: 0 ... The nullish coalescing operator can be seen as a special case of the logical OR (||) operator. The latter returns the right-hand side operand if the left operand is any falsy value, not only null or undefined.
๐ŸŒ
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?

๐ŸŒ
Futurestud.io
futurestud.io โ€บ tutorials โ€บ check-if-a-string-is-empty-in-javascript-or-node-js
Check if a String is Empty in JavaScript or Node.js
March 23, 2023 - This truthy check requires at least one character in the string to succeed. It evaluates to true if the value is not '' (empty string), undefined, null, false, 0, or NaN values.
๐ŸŒ
MSR
rajamsr.com โ€บ home โ€บ javascript string empty: how to avoid the dreaded error
JavaScript String Empty: How To Avoid The Dreaded Error | MSR - Web Dev Simplified
March 8, 2024 - One of the simplest ways to check if a string is empty is by utilizing the JavaScript string length property. This property returns the number of characters in a string. If the length is 0, it means the string is empty.
๐ŸŒ
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 - // function to check string is empty or not function checking(str) { if(str.replace(/\s/g,"") == "") { console.log("Empty String") } else{ console.log("Not Empty String") } } checking(" "); checking("Hello Javascript");
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ javascript โ€บ how do i check for an empty/undefined/null string in javascript?
How do I Check for an Empty/Undefined/Null String in JavaScript? | Sentry
This works because if one of the ... of the logical OR (||) operator is evaluated. To check for an empty string, the logical && operator is used....