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
๐ŸŒ
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 - The above code works for both ... 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....
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.

Discussions

Javascript empty string is not empty
Maybe its not an empty string, and instead a string of one or more invisible characters. const id = 'ยญ' // or '\u00AD' if stripped by reddit console.log(id) // '' console.log(id.trim().length) // 1 More on reddit.com
๐ŸŒ r/learnjavascript
12
3
March 2, 2022
What is the best way to check for a blank string?
To check if a string is blank in JavaScript all you wound need is this: if (foo){ //do stuff... } The if condition will evaluate "true" is foo is not null, undefined, NaN, empty string, 0, or false. http://stackoverflow.com/questions/5515310/is-there-a-standard-function-to-check-for-null-undefined-or-blank-variables-in More on reddit.com
๐ŸŒ r/learnprogramming
5
2
December 2, 2015
What is the best method to check if a variable is not null or empty?
It depends what you mean by empty, or how strict you want to be. These values will coerce to false: undefined null '' (empty string) 0 NaN Everything else coerces to true. So, if you are OK with rejecting all of those values, you can do: if(PostCodeInformation) { } If you want to make sure that PostCodeInformation is really an object value (and not a number or boolean, etc): if(typeof PostCodeInformation === 'object' && PostCodeInformation !== null) { } You have to do the null-check there, because in JavaScript typeof null returns 'object'. So dumb. If you want to make sure that PostCodeInformation has some property that you really need: if(PostCodeInformation && PostCodeInformation.myCoolProperty) { } Etc, etc More on reddit.com
๐ŸŒ r/javascript
18
3
August 2, 2015
jquery .text() function always returns an empty string
is env a element, id or a class? envText = $(this).find('env').text(); should it be: envText = $(this).find('.env').text(); ? More on reddit.com
๐ŸŒ r/learnjavascript
4
1
November 5, 2015
๐ŸŒ
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 tutorial, we'll explore the different ways of checking whether a string is empty or null in JavaScript and some best practices to follow when doing so.
๐ŸŒ
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 - In the first script, we create a function called isStringEmpty that accepts a single parameter, value. This function returns true if the value is either undefined, null, or an empty string (โ€œโ€).
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_string_methods.asp
JavaScript String Methods
1 week ago - If the separator is omitted, the returned array will contain the whole string in index [0]. If the separator is "", the returned array will be an array of single characters: ... For a complete reference to all JavaScript properties and methods, with full descriptions and many examples, go to:
๐ŸŒ
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.

๐ŸŒ
JavaScript in Plain English
javascript.plainenglish.io โ€บ identify-and-handle-empty-undefined-and-null-strings-in-javascript-like-a-pro-90ed75ab4a18
How to Check for Empty, Undefined, and Null Strings in JavaScript | JavaScript in Plain English
March 6, 2023 - Here is an example of using the == operator to check for an empty string: let str = ""; if (str == "") { console.log("The string is empty"); } else { console.log("The string is not empty"); } ... New JavaScript and Web Development content every day.
Find elsewhere
๐ŸŒ
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 - An empty string in JavaScript is represented by "" (a string with no characters). To check if a string is empty, you can use the strict equality operator (===) or the length property of the string.
๐ŸŒ
Attacomsian
attacomsian.com โ€บ blog โ€บ javascript-check-if-string-is-empty
How to check if a string is empty in JavaScript
October 23, 2022 - You can use the length property to check if a string is empty in JavaScript. If the string's length is equal to 0, then it is empty.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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 - Here are different approaches to check a string is empty or not. Using === operator we will check the string is empty or not.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ String โ€บ split
String.prototype.split() - JavaScript | MDN
If separator is a string, an Array of strings is returned, split at each point where the separator occurs in the given string. If separator is a regex, the returned Array also contains the captured groups for each separator match; see below for details. The capturing groups may be unmatched, in which case they are undefined in the array. If separator has a custom [Symbol.split]() method, its return value is directly returned. If separator is a non-empty string, the target string is split by all matches of the separator without including separator in the results.
๐ŸŒ
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 - To check for empty or whitespace-only strings, use text.trim().length === 0. 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 ...
๐ŸŒ
DEV Community
dev.to โ€บ akshatsoni26 โ€บ decoding-javascript-mastering-null-undefined-and-empty-values-hld
Decoding JavaScript: Mastering Null, Undefined, and Empty Values - DEV Community
August 4, 2024 - When checking for null or undefined, you can use value == null. Understanding the differences between null, undefined, empty strings, and empty arrays is crucial for writing clean and bug-free JavaScript code.
๐ŸŒ
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
The isEmpty function uses the equality operator (==) to check if the argument value is null or undefined. This works because if one of the operands is null or undefined, the other operand must be null or undefined for the equality comparison ...
๐ŸŒ
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:
๐ŸŒ
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 - Next, we check if str is not null. The null value represents the intentional absence of any object value. In javascript, By checking whether the string is null or empty, we ensure that the variable has a valid value.
๐ŸŒ
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 - Learn how to write a null check function in JavaScript and explore the differences between the null and undefined attributes.