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
1448

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 - We now know that an empty string is one that contains no characters. It is very simple to check if a string is empty.
๐ŸŒ
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.

๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_string_isempty.asp
Java String isEmpty() Method
The isEmpty() method checks whether a string is empty or not. This method returns true if the string is empty (length() is 0), and false if not. ... If you want to use W3Schools services as an educational institution, team or enterprise, send ...
๐ŸŒ
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.
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
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: ... function ...
๐ŸŒ
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 - When checking for empty, undefined, or null strings, be mindful of the nuances and edge cases in JavaScript: Use strict equality (===) for precise checks: This avoids unintended type coercion that might lead to incorrect evaluations. Be cautious with falsy values: Remember that 0, NaN, and false are also falsy but might be valid values in your application context. Consider using utility libraries: Libraries like Lodash offer utility functions, such as _.isEmpty, which can simplify and abstract these checks in a more readable manner.
๐ŸŒ
Roblog
robiul.dev โ€บ how-to-check-if-a-string-is-empty-in-javascript
How to Check if a String is Empty in JavaScript
June 4, 2023 - We can take advantage of this behavior to check if a string is empty using the negation operator (!). ... In this example, the isEmptyString function takes a string as an argument and returns true if the string is empty, and false otherwise.
๐ŸŒ
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 - For example, you have a form where a user can input their name. If the user doesn't input anything, the input field's value will be an empty string. However, the value will be null if the input field is not even created. JavaScript has several ways to check whether a string is empty or null.
๐ŸŒ
HCL Software
help.hcl-software.com โ€บ dom_designer โ€บ 9.0.1 โ€บ reference โ€บ r_wpdr_standard_string_isempty_r.html
isEmpty (JavaScript)
For an empty string, s=="" is true, but s==null is false. ... var cities = "Paris"; // Should not be empty if (cities.isEmpty()) { return "Empty"; } else { return "'" + cities + "'"; }
๐ŸŒ
MSR
rajamsr.com โ€บ home โ€บ javascript string empty: how to avoid the dreaded error
JavaScript String Empty: How To Avoid The Dreaded Error | MSR - Web Dev Simplified
March 8, 2024 - Here are three most common ways to handle empty strings in JavaScript: You can assign a default value to the string if it is empty, like this: str = str || โ€œdefaultโ€. This is a convenient and concise way to handle empty strings, but it may ...
๐ŸŒ
Shaikhu
shaikhu.com โ€บ how-to-check-if-a-string-is-null-blank-empty-or-undefined-using-javascript
How to check if a string is null, blank, empty or undefined using JavaScript? - shaikhu.com
August 10, 2021 - In above snippet, we are using JavaScript's arrow function to minimize lines of code. Here we have two checks, the first one will check for all null, empty and undefined strings and the second check is for white space characters. The above function will return true if a string is empty or false if not. Lets use some demo to check how this function works. ... 1 //function to check if a string is empty 2 const isEmpty = (str) => !str || !str.trim(); 3 4 const str1 = null; //null 5 const str2 = ""; //empty 6 const str3 = undefined; //undefined 7 const str4 = " "; //white space 8 const str5 = 0; //0 9 const str6 = "shaikhu"; //shaikhu 10 11 console.log(isEmpty(str1)); // true 12 console.log(isEmpty(str2)); // true 13 console.log(isEmpty(str3)); // true 14 console.log(isEmpty(str4)); // true 15 console.log(isEmpty(str5)); // true 16 console.log(isEmpty(str6)); // false
๐ŸŒ
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 (โ€œโ€).
๐ŸŒ
Squash
squash.io โ€บ how-to-check-for-an-empty-string-in-javascript
How To Check For An Empty String In Javascript
September 5, 2023 - 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. If the length of a string is 0, it means the string is empty.
๐ŸŒ
Futurestud.io
futurestud.io โ€บ tutorials โ€บ check-if-a-string-is-empty-in-javascript-or-node-js
Check if a String is Empty in JavaScript or Node.js
March 23, 2023 - import { Str } from '@supercharge/string' Str().isEmpty() Str('').isEmpty() Str(null).isEmpty() Str(' ').trim().isEmpty() // true Str(' ').isEmpty() Str('Future Studio').isEmpty() // false
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ String
String - JavaScript | MDN
String literals (denoted by double or single quotes) and strings returned from String calls in a non-constructor context (that is, called without using the new keyword) are primitive strings. In contexts where a method is to be invoked on a primitive string or a property lookup occurs, JavaScript will automatically wrap the string primitive and call the method or perform the property lookup on the wrapper object instead.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-check-empty-undefined-null-strings-in-javascript
How to check empty/undefined/null strings in JavaScript?
March 15, 2023 - In JavaScript, โ€œโ€ represents the empty string, and we can use the null keyword to initialize the string with a null value. If we donโ€™t assign any value to any variable, it is undefined by default. Sometimes, we need to check if the string is empty, u