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.
๐ŸŒ
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.
๐ŸŒ
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 ...
๐ŸŒ
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"); }
๐ŸŒ
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....
๐ŸŒ
Medium
medium.com โ€บ @python-javascript-php-html-css โ€บ how-to-check-for-empty-undefined-or-null-strings-in-javascript-d8f0bf514ead
How to Use JavaScript to Check for Null, Empty, or Undefined Strings
August 24, 2024 - In the first script, we create a function called isStringEmpty that accepts a single parameter, value. This function returns true if the value is either undefined, null, or an empty string (โ€œโ€).
Find elsewhere
๐ŸŒ
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?
๐ŸŒ
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.
๐ŸŒ
Coding Beauty
codingbeautydev.com โ€บ home โ€บ posts โ€บ how to check if a string is empty in javascript
How to Check if a String is Empty in JavaScript - Coding Beauty
July 18, 2022 - function checkIfEmpty(str) { if (str.trim().length === 0) { console.log('String is empty'); } else { console.log('String is NOT empty'); } } const str1 = 'not empty'; const str2 = ''; // empty const str3 = ' '; // contains only whitespace ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjavascript โ€บ javascript empty string is not empty
r/learnjavascript on Reddit: Javascript empty string is not empty
March 2, 2022 -

They closed my question on SO because it's not reproducible, but that's exactly why I posted, because code isn't behaving as it should.

Anyway, I 'm receiving a JSON result from a web service. It looks something like this:

{ "data": [{ "id": "123ABC", "name" : "Test 1" }, { "id": "", "name" : "Test 2" }] }

I 'm looping through the data array and need to determine if an id exists or not:

for( const item of data ) {
    if( item.id !== null && item.id.trim().length > 0 ) {
        doSomething();
    } else {
        doSomethingElse();
    }
}

My problem is that doSomething() fires for the first item ("123ABC") but also fires for the second where the id is empty.

I've tried spitting out the values for the second item:

console.log("NULL ", item.id === null);
console.log("EMPTY ", item.id.trim().length === 0);

and results are

NULL  false
EMPTY  false

so I'm wondering if there's something strange about the id value.

๐ŸŒ
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");
๐ŸŒ
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 ...
๐ŸŒ
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.length === 0) { console.log(`String is empty โœ…`) } else { console.log(`String is not empty โŒ`) } // String is empty โœ… ยท If the string contains leading or trailing whitespace, you should use the trim() method to remove whitespace before checking if it is empty:
๐ŸŒ
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.
๐ŸŒ
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 - This behavior may cause unexpected consequences if you consider 0, '', or NaN as valid values. js ยท const count = 0; const text = ""; const qty = count || 42; const message = text || "hi!"; console.log(qty); // 42 and not 0 console.log(message); // "hi!" and not "" The nullish coalescing operator avoids this pitfall by only returning the second operand when the first one evaluates to either null or undefined (but no other falsy values): js ยท const myText = ""; // An empty string (which is also a falsy value) const notFalsyText = myText || "Hello world"; console.log(notFalsyText); // Hello world const preservingFalsy = myText ??
๐ŸŒ
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?