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 › 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:
🌐
Quora
quora.com › What-does-an-empty-string-do-in-JavaScript
What does an empty string do in JavaScript? - Quora
Answer (1 of 4): Well, it’s either a String object or a string primitive, so it does all the same things any String or string does. As a String object, it knows all the String methods, even if some of them are pretty trivial when the value is an empty string.
🌐
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 - For example, you have a form where a user can input their name. If the user doesn't input anything, the input field's value will be an empty string. However, the value will be null if the input field is not even created. JavaScript has several ways to check whether a string is empty or null.
🌐
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
I’m the maintainer of the @supercharge/strings package providing convenient string utilities. The @supercharge/strings package comes with a handy Str#isEmpty method. This isEmpty method determines whether the wrapped value is an empty string.
🌐
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 - Understanding these distinctions is crucial for effective string handling and manipulation in JavaScript. An empty string in JavaScript is represented by "" (a string with no characters).
🌐
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.
Find elsewhere
🌐
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.
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-check-for-an-empty-string-in-javascript.php
How to Check for an Empty String in JavaScript
Topic: JavaScript / jQueryPrev|Next · 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 ...
🌐
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 - Empty strings contain no characters, while null strings have no value assigned. Checking for an empty, undefined, or null string in JavaScript involves verifying if the string is falsy or has a length of zero.
🌐
Squash
squash.io › how-to-check-for-an-empty-string-in-javascript
How To Check For An Empty String In Javascript
Before we dive into how to check for an empty string in JavaScript, let's define what an empty string is. In JavaScript, an empty string is a string that contains no characters. It is represented by a pair of double quotation marks ("") or a ...
🌐
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 - Using the trim() method ensures that strings containing only whitespace characters are treated as empty. It removes all whitespace from the beginning and end of the string, returning a new string without modifying the original.
🌐
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
The above code works for both null,undefined and empty string like "". Next, we are going to check if string is blank or having some white spaces. ... Here we are using JavaScript's trim() function to remove all the white spaces from both ends of the string and then check if its empty.
🌐
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 for an empty string, the logical && operator is used. The first operand uses the typeof operator to check if the argument value is a string. If the value is a string, leading and trailing white space and line terminator strings are ...
🌐
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 - Alternatively, we can access the length property of a string and compare its value with 0 to check if the string is empty. function checkIfEmpty(str) { if (str.length === 0) { console.log('String is empty'); } else { console.log('String is NOT ...
🌐
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 - Here are three most common ways to handle empty strings in JavaScript: You can assign a default value to the string if it is empty, like this: str = str || “default”. This is a convenient and concise way to handle empty strings, but it may ...
🌐
TutorialsPoint
tutorialspoint.com › how-to-check-empty-undefined-null-strings-in-javascript
How to check empty/undefined/null strings in JavaScript?
In JavaScript, “” represents the empty string, and we can use the null keyword to initialize the string with a null value. If we don’t assign any value to any variable, it is undefined by default. Sometimes, we need to check if the string is empty, u
🌐
W3docs
w3docs.com › javascript
How to Check for Empty/Undefined/Null String in JavaScript
Another option is checking the empty string with the comparison operator “===”. ... The JavaScript strings are generally applied for either storing or manipulating text. There is no separate for a single character.