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
5110

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
1446

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:
๐ŸŒ
DEV Community
dev.to โ€บ onlinemsr โ€บ 7-easy-ways-to-check-if-an-object-is-empty-in-javascript-ddm
7 Easy Ways To Check If An Object Is Empty In JavaScript - DEV Community
July 5, 2023 - If the object is empty, we print a message to the console โ€œThe object is empty.โ€ ยท If the JavaScript object is not empty, we print a message to the console โ€œThe object is not empty.โ€
๐ŸŒ
SamanthaMing
samanthaming.com โ€บ tidbits โ€บ 94-how-to-check-if-object-is-empty
How to Check if Object is Empty in JavaScript | SamanthaMing.com
const empty = {}; /* ------------------------- Plain JS for Newer Browser ----------------------------*/ Object.keys(empty).length === 0 && empty.constructor === Object // true /* ------------------------- Lodash for Older Browser ----------------------------*/ _.isEmpty(empty) // true ... A. Empty Object Check in Newer Browsers ... B. Empty Object Check in Older Browsers ... Vanilla JavaScript is not a new framework or library.
๐ŸŒ
SitePoint
sitepoint.com โ€บ blog โ€บ javascript โ€บ test for empty values in javascript
Test for Empty Values in Javascript โ€” SitePoint
November 6, 2024 - An empty function in JavaScript is a function that has been declared but does not perform any action or return any value. It is defined with the function keyword, followed by a set of parentheses and a pair of curly braces with no code inside. For example, function() {} is an empty function.
๐ŸŒ
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 first using the trim method to remove any leading or trailing whitespace characters from the str variable, then checking whether the resulting string has zero length.
๐ŸŒ
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.

Find elsewhere
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ javascript โ€บ how do i test for an empty javascript object?
How do I Test for an Empty JavaScript Object? | Sentry
This method was used as an alternative to using Object.keys before it was added to JavaScript in the 2011 ECMAScript 5 specification and is widely supported by browsers. You can use JSON.stringify() to convert the value to a JSON string to check if the value is an empty object.
๐ŸŒ
Built In
builtin.com โ€บ software-engineering-perspectives โ€บ javascript-check-if-object-is-empty
How to Check If an Object Is Empty in JavaScript | Built In
July 22, 2024 - We can also check this using Object.values and Object.entries. This is typically the easiest way to determine if an object is empty.
๐ŸŒ
Bonsaiilabs
bonsaiilabs.com โ€บ check-empty-object
How to check if a JavaScript Object is Empty - bonsaiilabs
That's because users is already null and the message is going to be displayed. If users is a valid object and the number of entries inside the object are zero. Since that is not the case, it didn't displayed any message.
๐ŸŒ
Roblog
robiul.dev โ€บ how-to-check-if-a-string-is-empty-in-javascript
How to Check if a String is Empty in JavaScript
June 13, 2023 - In the above code, the condition !str checks if the str variable is falsy (null, undefined, an empty string, or a 0). If it is, the code inside the if block will be executed, indicating that the string is empty. By incorporating these checks, we ensure proper handling of empty strings, including those with whitespace characters, null values, and undefined variables, in our 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
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....
๐ŸŒ
Ash Allen Design
ashallendesign.co.uk โ€บ blog โ€บ how-to-check-if-an-array-is-empty-in-javascript
How to Check If an Array Is Empty in JavaScript
January 8, 2024 - If the array is empty, the expression will return true like so: ... There are some caveats to using this approach, and we'll cover them further down in this article. A similar approach to the previous one is to use the length property with the ! operator. The ! operator is the logical NOT operator, and since in JavaScript 0 is a falsy value, we can use the !
๐ŸŒ
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 - Yes, but itโ€™s better to use === to avoid type conversion issues. ... Use the trim() method to remove whitespace from both ends of a string. ... No, JavaScript uses an empty string โ€œโ€ instead.
๐ŸŒ
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 โ€บ blog โ€บ how-to-check-if-an-array-is-empty-in-javascript
How to check if an array is empty in JavaScript? ยท CoreUI
February 7, 2024 - An astute question arises: why not rely solely on the length property to determine if an array is empty? The answer lies in JavaScriptโ€™s flexibility.
๐ŸŒ
Squash
squash.io โ€บ how-to-check-for-an-empty-string-in-javascript
How To Check For An Empty String In Javascript
Related Article: How to Use TypeScript with Next.js ยท One way to check for an empty string in JavaScript is by using the length property of the string. The length property returns the number of characters in a 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 - Discover the most efficient methods to check for an empty string in JavaScript. Learn best practices to ensure your code handles string validation effectively.
๐ŸŒ
Scaler
scaler.com โ€บ topics โ€บ check-if-object-is-empty-javascript
How to Check if an Object is Empty in JavaScript - Scaler Topics
December 8, 2022 - You can check if an object is empty using isEmptyObject(emptyObject). Take a look at the following code snippet to understand how we can use jQuery to check if an object is empty.