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 โ€บ 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. If it does, then we know that the string is empty. Otherwise, we know that the string is not empty. Here are some best practices to follow when checking for empty or null strings in JavaScript:
๐ŸŒ
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.

๐ŸŒ
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:
๐ŸŒ
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 - Another way to check if a string is empty is by comparing the string to an empty string. ... As with the previous method, if we have white spaces, this will not read the string as empty.
๐ŸŒ
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 - Considering JavaScript's type coercion and truthy/falsy evaluation, a more encompassing check can be performed to cover empty, undefined, and null strings in a single condition.
๐ŸŒ
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 - If we stringify the object and the result is simply an opening and closing bracket, we know the object is empty.
Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ programming-essentials โ€บ how-to-check-if-a-string-is-not-empty-d1410b77b909
How to Check If a String is Not Empty | by Cristian Salcescu | Frontend Essentials | Medium
May 21, 2021 - Here is the conversion of a variable containing an empty string. const text = ""; const isNotEmpty = Boolean(text); //false ยท We get the same result (false) when the variable is null or undefined. A variable that is declared and not initialized has the value of undefined. let text; const isNotEmpty = Boolean(text); //false ... Learn more on JavaScript, functional programming, and front-end development.
๐ŸŒ
Tutorial Republic
tutorialrepublic.com โ€บ faq โ€บ how-to-check-for-an-empty-string-in-javascript.php
How to Check for an Empty String in JavaScript
Topic: JavaScript / jQueryPrev|Next ยท You can use the strict equality operator (===) to check whether a string is empty or not. The comparsion str === "" will only return true if the data type of the value is string and it is not empty, otherwise ...
๐ŸŒ
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 - When the object is not null or undefined, we check if the object is empty using the Object.keys() method. 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.โ€
๐ŸŒ
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.
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ javascript-check-if-string-is-empty
How to check if a String is Empty in JavaScript | bobbyhadz
You can also check that the value isn't equal to an empty string. ... Copied!const str = ''; if (typeof str === 'string' && str !== '') { console.log('The string is NOT empty'); } else { // ๐Ÿ‘‡๏ธ this runs console.log('The string is empty'); }
๐ŸŒ
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 - The easiest way to check if a JavaScript array is empty is to use the length property with the === operator. The length property returns the number of items in the array as an integer.
๐ŸŒ
SamanthaMing
samanthaming.com โ€บ tidbits โ€บ 94-how-to-check-if-object-is-empty
How to Check if Object is Empty in JavaScript | SamanthaMing.com
And we're covered for null and undefined. It will return false and not throw a TypeError. isObjectEmpty(null); // false isObjectEmpty(undefined); // false ยท There are tons of external libraries you can use to check for empty objects.
๐ŸŒ
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 (โ€œโ€).
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ check-if-an-object-is-empty-in-javascript
How to Check if an Object is Empty in JavaScript โ€“ JS Java isEmpty Equivalent
November 7, 2024 - Letโ€™s now understand these and more options you can use to check if an object is empty in JavaScript. The Object.keys() method is a static object method introduced in ECMAScript6 (ES6) and is supported in all modern browsers. This method returns an array with the keys of an object. For example: let userDetails = { name: "John Doe", username: "jonnydoe", age: 14 }; console.log(Object.keys(userDetails)); // ["name","username","age"] With this, you can now apply the .length property. If it returns zero (0), the object is empty.
๐ŸŒ
Squash
squash.io โ€บ how-to-check-for-an-empty-string-in-javascript
How To Check For An Empty String In Javascript
However, it's worth noting that this method will also consider a string consisting of whitespace characters as non-empty. If you want to treat whitespace-only strings as empty, you can trim the string before checking its length. The trim() method ...
๐ŸŒ
Tomekkolasa
tomekkolasa.com โ€บ how-to-check-if-object-is-empty-in-javascript
How to check if an object is empty in JavaScript | Tomek Kolasa | Deep dive into a full-stack JavaScript
July 9, 2020 - Here we created our own utility function isObjectEmpty() which we can use to check if an object is empty. This is also a nice solution. The isObjectEmpty name makes the purpose of the function clear. Moreover, the implementation is also easy to understand. We loop through all object properties (own and inherited) using a for-in loop. On each iteration we call the objectโ€™s .hasOwnProperty() method. We use it to check if the property actually belongs to this object and not to one of its ancestors up the prototype chain.