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.

🌐
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.”
Discussions

How can I check if a variable is empty in JavaScript? - LambdaTest Community
For example, in the case of response.photo from a JSON object, how can I check if it’s empty, especially when it may contain empty data cells? More on community.lambdatest.com
🌐 community.lambdatest.com
0
April 4, 2025
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
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
October 10, 2015
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
October 14, 2017
🌐
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:
🌐
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.
🌐
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....
Find elsewhere
🌐
LambdaTest Community
community.lambdatest.com › general discussions
How can I check if a variable is empty in JavaScript? - LambdaTest Community
April 4, 2025 - For example, in the case of response.photo from a JSON object, how can I check if it’s empty, especially when it may contain empty data cells?
🌐
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 !
🌐
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.
🌐
Quora
quora.com › How-do-you-check-if-an-HTML-element-is-empty-using-JavaScript
How to check if an HTML element is empty using JavaScript - Quora
Answer (1 of 2): First of all, you need to define empty. If you want to check that there is nothing inside it (like in Mohamed Nabeel’s answer), you can write a function like this: [code]function isEmpty(element) { return element.innerHTML === '' } [/code]With the function above the results ar...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array
Array - JavaScript | MDN
September 28, 2025 - 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.
🌐
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.
🌐
Scaler
scaler.com › topics › check-if-object-is-empty-javascript
How to Check if an Object is Empty in JavaScript - Scaler Topics
January 5, 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.
🌐
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.
🌐
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.
🌐
Squash
squash.io › how-to-check-for-an-empty-string-in-javascript
How To Check For An Empty String In Javascript
September 5, 2023 - 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.
🌐
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 - 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.