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.

🌐
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 - We now know that an empty string is one that contains no characters. It is very simple to check if a string is empty.
Discussions

How can I verify if a string is empty, undefined, or null in JavaScript?
Does JavaScript have an equivalent of string.Empty, or do I need to check for "" manually? More on community.latenode.com
🌐 community.latenode.com
2
October 4, 2024
Javascript check string empty?
Is there a universal JavaScript function that checks that a string is empty or has a value? I can do this with PHP but with JavaScript, I am new to it. More on forumweb.hosting
🌐 forumweb.hosting
6
January 14, 2017
Javascript: "Empty string is not a number." Also Javascript: "Empty string is not not a number."

Consider that example in C.

parseFloat(''); Would likely give NaN or NULL. isNaN(''); Would definitely return false.

I get it, "lel JS sux", but let's think more critically. We're suppose to be programmers.

More on reddit.com
🌐 r/ProgrammerHumor
12
58
December 12, 2013
JavaScript String.split(RegExp) is returning empty strings
So, I believe that what's going on here is that the Javascript you are running is using the split method, while the regex is matching. Split is actually "splitting" the string into parts. So when it matches the dot . in your regex, it splits it into document, ., write, . Your regex, on the other hand matches the dot .. You see that just with the first character, you're already skewed 3 to 1. If you're wanting to replicate the functionality of what you have on regex101, you'd want to use the match instead. Here is a demo More on reddit.com
🌐 r/regex
5
2
July 19, 2017
🌐
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.

🌐
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....
🌐
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 - JS Tutorial · Web Tutorial · A to Z Guide · Projects · OOP · DOM · Set · Map · Math · Number · Boolean · Exercise · Last Updated : 11 Jul, 2025 · Empty strings contain no characters, while null strings have no value assigned.
🌐
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.
Find elsewhere
🌐
Roblog
robiul.dev › how-to-check-if-a-string-is-empty-in-javascript
How to Check if a String is Empty in JavaScript
June 4, 2023 - It's crucial to have reliable techniques to check if a string is empty in JavaScript, whether you're validating user input, processing data, or ...
🌐
ForumWeb Hosting
forumweb.hosting › home › forums › web design & development › web programming
Javascript check string empty?
January 14, 2017 - Say, if a string is empty var name = "" then console.log(!name) returns true .
🌐
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).
🌐
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 checking whether the str variable is a string and whether its length is zero. If it is, then we know that it's an empty string. If the str variable is null, then we know that it's a null string.
🌐
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 - Want to check if a string is empty in JavaScript? There are several ways to do it, but not all are equally readable, safe, or performant. In...
🌐
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.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String
String - JavaScript | MDN
In contexts where a method is to be invoked on a primitive string or a property lookup occurs, JavaScript will automatically wrap the string primitive and call the method or perform the property lookup on the wrapper object instead. js ·
🌐
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>
🌐
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 - 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.
🌐
W3Schools
w3schools.com › java › ref_string_isempty.asp
Java String isEmpty() Method
This method returns true if the string is empty (length() is 0), and false if not.
🌐
Squash
squash.io › how-to-check-for-an-empty-string-in-javascript
How To Check For An Empty String In Javascript
In JavaScript, an empty string is a string that contains no characters. It is represented by a pair of double quotation marks ("") or a pair of single quotation marks (''). An empty string is different from a null or undefined value.
🌐
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 - ... Use the trim() method to remove whitespace from both ends of a string. ... No, JavaScript uses an empty string “” instead. How do you validate a string using regular expressions?
🌐
LogRocket
blog.logrocket.com › home › how to check for null, undefined, or empty values in javascript
How to check for null, undefined, or empty values in JavaScript - LogRocket Blog
February 14, 2025 - We can check for this by doing the following: ... This depends on the object’s “truthiness”. “Truthy” values like “words” or numbers greater than zero would return true, whereas empty strings would return false.