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
1447

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.

🌐
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 - This approach takes advantage of the fact that an empty string (""), undefined, and null are all falsy values in JavaScript. Thus, the !myString condition checks for all three cases simultaneously.
🌐
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 - Another way to check if a string is empty is by comparing the string to an empty string. ... As with the previous method, if we have white spaces, this will not read the string as empty. So we must first use the trim() method to remove all forms of whitespace:
🌐
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
To check that a value is not an empty string, null, or undefined, you can create a custom function that returns true if a value is null, undefined, or an empty string and false for all other falsy values and truthy values:
🌐
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. ... Follow Łukasz Holeczek on GitHub Connect with Łukasz Holeczek on LinkedIn Follow Łukasz Holeczek on X (Twitter) Łukasz Holeczek, Founder of CoreUI, is a seasoned Fullstack Developer and entrepreneur with over 25 years of experience. As the lead developer for all JavaScript, React.js, and Vue.js products at CoreUI, they specialize in creating open-source solutions that empower developers to build better and more accessible user interfaces.
🌐
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.

🌐
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 - JavaScript has several ways to check whether a string is empty or null. Let's explore some of them. One way to check for an empty or null string is to use the if statement and the typeof operator.
Find elsewhere
🌐
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 - You can check for an empty string using value === β€œβ€. What is the difference between null and undefined in JavaScript? null represents the intentional absence of a value, whereas undefined indicates a variable has been declared but not assigned ...
🌐
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.
🌐
Medium
medium.com β€Ί programming-essentials β€Ί how-to-check-if-a-string-is-not-empty-d1410b77b909
How to Check If a String is Not Empty | by Cristian Salcescu | Frontend Essentials | Medium
May 21, 2021 - ... When checking to see if a string is not empty we can start from the obvious test condition (text !== ""). const text = "Hi!";console.log(text !== "") //true Β· This condition does not include checks for null or undefined.
🌐
Bobby Hadz
bobbyhadz.com β€Ί blog β€Ί javascript-check-if-string-is-empty
How to check if a String is Empty in JavaScript | bobbyhadz
Copied!const str = undefined; if (str?.trim()) { console.log('The string is NOT empty'); } else { // πŸ‘‡οΈ this runs console.log('The string is empty'); } Instead of getting an error for trying to call the trim() method on an undefined value, the call short-circuits, returning undefined because we used the optional chaining (?.) operator. Use the value in an if statement to check if it is truthy.
🌐
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
The validation steps could require ... length for a field. In other cases you may only ensure the value is not an empty string. This tutorial walks you through the steps of detecting whether a given string value is empty. ... The first step in determining if a String is empty is to make sure the given value is actually a string. Please have a look at this tutorial on checking if a value ...
🌐
TutorialsPoint
tutorialspoint.com β€Ί how-to-check-empty-undefined-null-strings-in-javascript
How to check empty/undefined/null strings in JavaScript?
We can convert the strings to boolean using the Boolean constructor or the Double Not operator (!!). When we convert any variable to the Boolean, it maps to the false for all falsy values and true for other values. In JavaScript, empty string, null, and undefined are falsy values, so when we convert it to Boolean, the Boolean() constructor always returns false. In the syntax below, we used the Boolean() constructor to convert the string to a boolean value and check if it's empty.
🌐
W3docs
w3docs.com β€Ί javascript
How to Check for Empty/Undefined/Null String in JavaScript
When the string is not null or undefined, and you intend to check for an empty one, you can use the length property of the string prototype, as follows: ... Another option is checking the empty string with the comparison operator β€œ===”. ...
🌐
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 - A string in JavaScript is considered truthy unless it is empty (β€˜β€™), null, or undefined, which are all falsy values. This behavior underpins many of the shorthand techniques used for validation but also requires a clear understanding to avoid unintended consequences. For example, a simple `if` statement like `if (string)` may suffice for most checks, but it does not differentiate between an empty string and a string that is literally undefined or null.
🌐
Shaikhu
shaikhu.com β€Ί how-to-check-if-a-string-is-null-blank-empty-or-undefined-using-javascript
How to check if a string is null, blank, empty or undefined using JavaScript? - shaikhu.com
In above snippet, we are using JavaScript's arrow function to minimize lines of code. Here we have two checks, the first one will check for all null, empty and undefined strings and the second check is for white space characters. The above function will return true if a string is empty or false if not...
🌐
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 - To check for this, use the string if statement directly, like this: function checkIfEmpty(str) { if (str) { console.log('String is NOT empty'); } else { console.log('String is empty'); } } const str1 = 'not empty'; const str2 = ''; // empty ...