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.

๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ API โ€บ Selection โ€บ empty
Selection: empty() method - Web APIs | MDN
The Selection.empty() method removes all ranges from the selection, leaving the anchorNode and focusNode properties equal to null and nothing selected. When this method is called, a selectionchange event is fired at the document.
๐ŸŒ
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.

๐ŸŒ
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
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.
๐ŸŒ
SamanthaMing
samanthaming.com โ€บ tidbits โ€บ 94-how-to-check-if-object-is-empty
How to Check if Object is Empty in JavaScript | SamanthaMing.com
It's just regular, plain JavaScript without the use of a library like Lodash or jQuery. We can use the built-in Object.keys method to check for an empty object.
๐ŸŒ
HCL Software
help.hcl-software.com โ€บ dom_designer โ€บ 9.0.1 โ€บ reference โ€บ r_wpdr_standard_string_isempty_r.html
isEmpty (JavaScript)
Checks whether a string is empty. String (Standard - JavaScript) isEmpty() : boolean ยท An empty string has a length of 0. For an empty string, s=="" is true, but s==null is false. (1) This example returns the content of cities. var cities = "Paris"; // Should not be empty if (cities.isEmpty()) ...
Find elsewhere
๐ŸŒ
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.โ€
๐ŸŒ
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.
๐ŸŒ
Playcode
playcode.io โ€บ empty_javascript
Empty Javascript Playground
The #1 JavaScript playground to write, run and test code instantly. Free JS sandbox with live preview, npm packages, and AI coding assistant. No setup required.
๐ŸŒ
jQuery
api.jquery.com โ€บ empty
.empty() | jQuery API Documentation
Description: Remove all child nodes of the set of matched elements from the DOM ยท This method removes not only child (and other descendant) elements, but also any text within the set of matched elements. This is because, according to the DOM specification, any string of text within an element ...
๐ŸŒ
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>
๐ŸŒ
W3Schools
w3schools.com โ€บ jquery โ€บ html_empty.asp
jQuery empty() Method
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ javascript-check-if-string-is-empty
How to check if a String is Empty in JavaScript | bobbyhadz
Copied!const str = 'bobbyhadz.com'; if (!str) { console.log( 'The value is undefined, null 0, false, NaN or empty string', ); } else { // ๐Ÿ‘‰๏ธ str is NOT "", undefined, null, 0, false, NaN console.log('The string is truthy'); } The if block is only run if the str variable stores a falsy value. The falsy values in JavaScript are: false, 0, empty string "", null, undefined and NaN (not a number).
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
Mastering JS
masteringjs.io โ€บ tutorials โ€บ fundamentals โ€บ empty-object-truthy
Empty Objects are Truthy in JavaScript? - Mastering JS
You can use the Object.keys() function to check if an object is empty as shown below. if ({}) { console.log('I will print'); } if (Object.keys({}).length === 0) { console.log('I will not print'); } JavaScript throws an error if you call ...
๐ŸŒ
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...