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 › 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.
Discussions

Falsy values vs null, undefined, or empty string
I've worked with jQuery over the years. However, recently, I've found myself getting deeper into the JavaScript language. Recently, I've heard about "truthy" and falsey values. However, I don't fully More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
Should I use 'null' or empty string?
It depends on how you are going to use the variable. Sometimes you need to distinguish between an empty string and a string which hasn't been assigned a value, in which case using an empty string probably will not work. More on reddit.com
🌐 r/learnprogramming
7
1
October 10, 2018
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 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
🌐
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.

🌐
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 - No, JavaScript uses an empty string “” instead. How do you validate a string using regular expressions? Use the test() method with a regular expression to validate a string. ... Validator.js is a library providing various string validation ...
🌐
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 - This truthy check requires at least one character in the string to succeed. It evaluates to true if the value is not '' (empty string), undefined, null, false, 0, or NaN values.
🌐
TutorialsPoint
tutorialspoint.com › how-to-check-empty-undefined-null-strings-in-javascript
How to check empty/undefined/null strings in JavaScript?
March 15, 2023 - 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, undefined, or null while working with the strings.
🌐
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 - Before we delve into checking for empty, undefined, or null strings, let's clarify what undefined and null signify in JavaScript: undefined means a variable has been declared but has not yet been assigned a value. null is an assignment value that represents the intentional absence of any object value.
Find elsewhere
🌐
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 - let myStr = " "; if (myStr.tri... is NOT an empty string!"); } ... Note: If the value is null, this will throw an error because the length property does not work for null....
🌐
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.
🌐
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 ... the right side of the logical OR (||) operator is evaluated. To check for an empty string, the logical && operator is used....
🌐
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 - Are you tired of always getting that pesky error message when your JavaScript code tries to access an empty, undefined, or null string? Fear not, for in this article we will discuss various methods for checking for these conditions in JavaScript to help you avoid those error messages and get your code working smoothly. It is generally not recommended to use the double equals (==) or not equals (!=) operator to check for an empty, undefined, or null string in JavaScript.
🌐
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.
🌐
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 - This is the same approach we use in CoreUI components for form validation, conditional rendering, and data processing across our component ecosystem. To check for empty or whitespace-only strings, use text.trim().length === 0. For a more ...
🌐
Gabrielcordeiro
gabrielcordeiro.dev › blogs › what's the difference between null, undefined and empty in javascript
What's the difference between Null, Undefined and Empty in Javascript | Gabriel Schmidt Cordeiro
February 16, 2024 - An empty string is a string with zero characters. While it might seem similar to null or undefined, it is a valid value and can be explicitly assigned to a variable.
🌐
Medium
medium.com › @yi_yuan › understanding-null-undefined-and-empty-strings-in-javascript-a07959084d
Understanding Null, Undefined, and Empty Strings in JavaScript | by Yi Yuan | Medium
January 8, 2023 - An empty string is a string that has nothing inside it. It’s like a word that has no letters. Here’s an example: let favoriteFood = " "; console.log(favoriteFood); // logs an empty string · It’s important to understand the differences ...
Top answer
1 of 5
24

In programming, truthiness or falsiness is that quality of those boolean expressions which don't resolve to an actual boolean value, but which nevertheless get interpreted as a boolean result.

In the case of C, any expression that evaluates to zero is interpreted to be false. In Javascript, the expression value in

if(value) {
}

will evaluate to true if value is not:

null
undefined
NaN
empty string ("")
0
false

See Also
Is there a standard function to check for null, undefined, or blank variables in JavaScript?

2 of 5
9

The set of "truthy" and "falsey" values in JavaScript comes from the ToBoolean abstract operation defined in the ECMAScript spec, which is used when coercing a value to a boolean:

+--------------------------------------------------------------------------+
| Argument Type | Result                                                   |
|---------------+----------------------------------------------------------|
| Undefined     | false                                                    |
|---------------+----------------------------------------------------------|
| Null          | false                                                    |
|---------------+----------------------------------------------------------|
| Boolean       | The result equals the input argument (no conversion).    |
|---------------+----------------------------------------------------------|
| Number        | The result is false if the argument is +0, −0, or NaN;   |
|               | otherwise the result is true.                            |
|---------------+----------------------------------------------------------|
| String        | The result is false if the argument is the empty String  |
|               | (its length is zero); otherwise the result is true.      |
|---------------+----------------------------------------------------------|
| Object        | true                                                     |
+--------------------------------------------------------------------------+

From this table, we can see that null and undefined are both coerced to false in a boolean context. However, your fields.length === 0 does not map generally onto a false value. If fields.length is a string, then it will be treated as false (because a zero-length string is false), but if it is an object (including an array) it will coerce to true.

If fields should be a string, then !fields is a sufficient predicate. If fields is an array, your best check might be:

if (!fields || fields.length === 0)
🌐
Arunkumar Blog
arungudelli.com › home › tutorial › javascript › how to check if a string is empty/null/undefined in javascript
How To Check If A String Is Empty/Null/Undefined In JavaScript
November 17, 2019 - To check if a string is null or empty or undefined use the following code snippet if(!emptyString){ // String is empty }
🌐
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 snippet will print "This is empty, i.e. either null or undefined". The above code works for both null,undefined and empty string like "".