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
๐ŸŒ
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.

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 article, you will learn how to check if a sting is empty or null in JavaScript. We will see many examples and methods you can use so that you can understand them and decide which one to use and when. Before we begin, you need to understand what the terms Null and Empty mean, and understand that they are not synonymous. For example, if we declare a variable and assign it an empty string, and then declare another variable and assign it the Null value, we can tell them apart by looking at their datatype:
๐ŸŒ
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?
October 31, 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?

๐ŸŒ
Reddit
reddit.com โ€บ r/reactjs โ€บ use empty string, null or remove empty property in api request/response?
r/reactjs on Reddit: Use empty string, null or remove empty property in API request/response?
January 31, 2023 -

we had an argue about this on our team, I wonder what approach should we consider? what is the better?

We have a user interface like this:

interface User {
name:string;
family?:string;
age:number;

family name is optional, so our backend developer sends us this json data for those users that didnt filled the family:

{name: "john",
family:"",
age:18
}

and in another object's case they sends null for the optional props...

We have been confused with this not having a solid convention from the backend team! what approach is the better? I think the right way is just removing optional property from the json data instead of setting it to null or "" empty string, I mean just dont add that!

๐ŸŒ
Reddit
reddit.com โ€บ r/learnjavascript โ€บ checking if property in object is an empty string
r/learnjavascript on Reddit: Checking if property in object is an empty string
May 22, 2022 -

Hello and sorry for my noob question but i am stuck here and cant move forward

i have this object

const obj = { first: "", second: "", third: "", fourth: "" };

and using useState to update it const [urls, setUrls] = useState(obj);

and trying in switch to check if properties are empty or not like this:

switch(urls){
case urls.first === '':
setUrls({ ...urls, ["first"]: state.favoritesUrl});
setText({ ...text, ["first"]: state.favoritesUrl});
console.log("ispis urls.first: ",text.first);
break;
case urls.second.length === 0:
setUrls({ ...urls, ["second"]: state.favoritesUrl});
setText({ ...text, ["second"]: state.favoritesUrl});
console.log("ispis urls.second: ",text.second);
break;
case urls.third.length === 0:
setUrls({ ...urls, ["third"]: state.favoritesUrl});
setText({ ...text, ["third"]: state.favoritesUrl});
console.log("ispis urls.third: ",text.third);
break;
case urls.fourth.length === 0:
setUrls({ ...urls, ["fourth"]: state.favoritesUrl});
setText({ ...text, ["fourth"]: state.favoritesUrl});
console.log("ispis urls.fourth: ",text.fourth);
break;

i tried with .length === 0

and with Object.key(urls.first).length === 0

but my switch/case wont trigger.

๐ŸŒ
Reddit
reddit.com โ€บ r/programmerhumor โ€บ empty vs null string
r/ProgrammerHumor on Reddit: Empty vs Null String
May 26, 2023 - Not necessarily. Empty string often means it's known to be empty while null means unknown or not set. ... Just an example where there's a practical benefit for differentiating between the two.
Find elsewhere
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ javascript-check-if-string-is-empty
How to check if a String is Empty in JavaScript | bobbyhadz
The if block in the example runs if the str variable stores undefined, null or an empty string. Conversely, you can use the logical AND (&&) operator to check if a variable doesn't store an empty string, null or undefined.
๐ŸŒ
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.
๐ŸŒ
Reddit
reddit.com โ€บ r/regex โ€บ javascript string.split(regexp) is returning empty strings
r/regex on Reddit: JavaScript String.split(RegExp) is returning empty strings
July 19, 2017 -

So I have this regular expression:

/([\s+\-*\/%=&^|<>~"`!;:,.?()[\]{}\\])/g

which should match all whitespace, and +-*/%=&|<>~"`!;:,.?()[]{}]\

(and the up caret, but reddit is being annoying)

On regex websites like this one, you can see that the Regular Expression works how I'd like it to, however if you run the JavaScript String.split, it returns all the matches I want, and the text in between matches which I also want. The issue is it also returns some empty strings, which I don't want.

Run this in your browser and you'll see what I mean:

`document.write("<h2>Table of Factorials</h2>");
for(i = 1, fact = 1; i < 10; i++, fact *= i) {
    document.write(i + "! = " + fact);
    document.write("<br>");
}`.split(/([\s+\-*\/%=&^|<>~"`!;:,.?()[{\]}\\])/g);

The expected result is something like:

["document", ".", "write", "(", """, "<", "h2", ">", "Table", " ", "of", " ", "Factorials", "<", "/", "h2", ">", """, ")"...]

however, the actual result is:

["document", ".", "write", "(", "", """, "", "<", "h2", ">", "Table", " ", "of", " ", "Factorials", "<", "", "/", "h2", ">", "", """, "", ")"...]

Why am I getting some empty strings back? How can I fix it? Thank you for your time.

๐ŸŒ
W3Resource
w3resource.com โ€บ javascript-exercises โ€บ javascript-string-exercise-2.php
JavaScript validation with regular expression: Check whether a string is blank or not - w3resource
// Define a function called is_Blank that checks if the input string is blank is_Blank = function(input) { // Check if the length of the input string is 0 if (input.length === 0) // If the length is 0, return true indicating that the 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...
๐ŸŒ
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 - From my extensive expertise, the most reliable and performant solution is checking the stringโ€™s length property against zero. This approach is direct, efficient, and handles the specific case of empty strings without ambiguity.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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: