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
1447

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.

🌐
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.
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
Differentiating between 0 and empty field with parseFloat.
Is it possible my problem is that parseFloat returns 0.0 as NaN? Just try it in the console (F12 -> Console). > parseFloat('0.0') 0 More on reddit.com
🌐 r/javascript
5
1
August 30, 2017
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 9, 2015
[AskJS] Is it worth it to delete empty array instances?

I can't say for certain because I'm not a VM wizard, but I'm guessing all that deleting those does is remove the reference, and the object your referencing itself is just marked for deletion by the GC later on either way. The VM is keeping a pointer to that object anyways, so you aren't really saving significant memory. I'd say don't bother, memory is cheap and 200-300 indices is nothing (though I'd need more context to know how much memory those are actually taking)

More on reddit.com
🌐 r/javascript
56
5
April 16, 2017
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-check-an-object-is-empty-using-javascript
How to Check an Object is Empty using JavaScript? - GeeksforGeeks
July 11, 2025 - If the length of array is 0, then object is empty. JavaScript · let obj = {}; if (Object.keys(obj).length === 0) { console.log("Empty Object") } else { console.log("Not Empty Object") } Output ·
🌐
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.
🌐
Zipy
zipy.ai › blog › how-do-i-test-for-an-empty-javascript-object
how do i test for an empty javascript object
April 12, 2024 - Throughout this article, we'll explore various techniques for checking if an object is empty in JavaScript. We'll cover built-in methods, custom functions, and best practices to ensure efficient and effective object handling.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › some
Array.prototype.some() - JavaScript | MDN
4 days ago - If such an element is found, some() immediately returns true and stops iterating through the array. Otherwise, if callbackFn returns a falsy value for all elements, some() returns false. Read the iterative methods section for more information about how these methods work in general. some() acts like the "there exists" quantifier in mathematics. In particular, for an empty array, it returns false for any condition.
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 - 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:
🌐
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
December 15, 2022 - 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.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › Selection › empty
Selection: empty() method - Web APIs | MDN
const log = document.getElementById("log"); // The selection object is a singleton associated with the document const selection = document.getSelection(); // Logs if there is a selection or not function newSelectionHandler() { if (selection.rangeCount !== 0) { log.textContent = "Some text is selected."; } else { log.textContent = "No selection on this document."; } } document.addEventListener("selectionchange", () => { newSelectionHandler(); }); newSelectionHandler(); // The button cancel all selection ranges const button = document.querySelector("button"); button.addEventListener("click", () => { selection.empty(); });
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array
Array - JavaScript | MDN
5 days ago - Array elements are object properties in the same way that toString is a property (to be specific, however, toString() is a method). Nevertheless, trying to access an element of an array as follows throws a syntax error because the property name is not valid: ... JavaScript syntax requires properties beginning with a digit to be accessed using bracket notation instead of dot notation.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › Empty
Empty statement - JavaScript | MDN
July 20, 2025 - An empty statement is used to provide no statement, although the JavaScript syntax would expect one.
🌐
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 !
🌐
DEV Community
dev.to › smpnjn › how-to-check-if-object-is-empty-in-javascript-5afl
How to Check if Object is Empty in JavaScript - DEV Community
February 5, 2023 - For example, {} is an empty object, but how do we actually test that this is the case? ... The easiest (and best) way to do this, is to use Object.keys(). This method turns all the keys in an object to an array, which we can then test the length of: let myObject = {} console.log(Object.keys(myObject).length) // Returns 0! But wait... Javascript is well known for how it handles types strangely - and new constructors return an object with length 0:
🌐
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.

🌐
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
We can also check this using Object.values and Object.entries. This is typically the easiest way to determine if an object 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.
🌐
Scaler
scaler.com › home › topics › how to check if an object is empty in javascript?
How to Check if an Object is Empty in JavaScript - Scaler Topics
January 6, 2024 - 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.
🌐
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.”
🌐
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.