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
1448

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:
Discussions

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
Determining if a string contains only whitespace using RegExp. Getting unexpected results.

Use a regex literal /^\s+$/ instead of new RegExp and I think it should work. If you create a regex from a string like in your example, you need to double escape the \ so it'd be \\s instead.

More on reddit.com
🌐 r/learnjavascript
2
1
August 28, 2014
isNaN("") returns false?
Which isNaN are you using? The global version, or Number.isNaN? If you're using the global version, you should know that it comes with an implicit coercion to Number. So, Number("") === 0, and 0 is not NaN, so you get false. More reading here . More on reddit.com
🌐 r/javascript
23
16
August 28, 2016
Split string using ES6 Spread 🎉
I might be sold school since I prefer the former. The old syntax is self documenting, while the new is just confusing. More on reddit.com
🌐 r/webdev
241
847
April 21, 2018
🌐
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.

🌐
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.
🌐
Medium
medium.com › @mhangoyewo › how-an-empty-string-in-javascript-ruined-my-afternoon-29a603cd3d19
How an Empty String in JavaScript Ruined My Afternoon | by Mhango Yewo | Medium
January 21, 2024 - Who would have thought that calling Number with an empty string would actually return 0? By then, I considered myself to be pretty well-versed with the various tricky behaviours of JavaScript's type coersion, but it turns out that that was not quite enough.
Find elsewhere
🌐
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 ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String
String - JavaScript | MDN
Strings can be created as primitives, from string literals, or as objects, using the String() constructor: js · const string1 = "A string primitive"; const string2 = 'Also a string primitive'; const string3 = `Yet another string primitive`; js · const string4 = new String("A String object"); String primitives and string objects share many behaviors, but have other important differences and caveats.
🌐
Sam Jarman
samjarman.co.nz › blog › empty-string
Empty String Considered Harmful
July 30, 2024 - By properly expressing to the compiler through the language that this string can be optional, we get the compiler helping us all the way through our code. By inventing our own indicator for optional strings - the empty string - we get no such help, and bugs find their way in very quickly, even with guards such as documentation.
🌐
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.
🌐
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 - Always use triple equals (===) when comparing a string to null. This ensures that the types are checked, and you don't accidentally compare a string to the number 0 or false. Use strict equality (===) when checking for an empty string.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › Empty
Empty statement - JavaScript | MDN
July 20, 2025 - An empty statement is used to provide no statement, although the JavaScript syntax would expect one.
🌐
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>
🌐
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.
🌐
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.
🌐
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.
🌐
Squash
squash.io › how-to-check-for-an-empty-string-in-javascript
How To Check For An Empty String In Javascript
September 5, 2023 - The trim() method is a built-in method in JavaScript that removes whitespace characters from both ends of a string. By combining the trim() method with the length property, we can effectively check for an empty string, including cases with ...
🌐
W3Schools
w3schools.com › js › js_string_methods.asp
W3Schools.com
2 weeks ago - It allows the use of negative indexes while charAt() do not. Now you can use myString.at(-2) instead of charAt(myString.length-2). ... If no character is found, [ ] returns undefined, while charAt() returns an empty string.
🌐
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
August 10, 2021 - 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.
🌐
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 ...