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.

๐ŸŒ
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.
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
Easiest way to check for null and empty string on a TypeScript number - Stack Overflow
I'm surprised this question hasn't been asked, so maybe I'm overlooking the obvious. I have a form field that is supposed to be a number. Its starting value is null, but once a number is entered and More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to Create a Type That Rejects Empty String?
IMO, the best way to do this is to use a "branded type" with a constructor function that validates at runtime. This means that anywhere you want to make a NonEmptyString type, you'll have to handle the potential error. declare const opaqueSym: unique symbol; export type NonEmptyString = string & { [opaqueSym]: "NonEmptyString" }; export function NonEmptyString(s: string): NonEmptyString { if (s.length === 0) { throw new Error("NonEmptyString: received an empty string") } return s as NonEmptyString } You could also do a custom type guard: export function isNonEmptyString(s: string): s is NonEmptyString { return s.length > 0; } All that said, this is probably way more effort than necessary when you can just check length at runtime, unless you're composing a bunch of functions that depend on the length being greater than zero. ETA: You can see how a library like Zod can be used to achieve this as well: https://zod.dev/?id=brand And just to restate that last line: the main benefit of using types like this is to make a promise to consuming code that some validation has taken place on a piece of data. It can be useful if you need to mark something as "validated" that might be consumed in many places, and/or if you are validating the same kind of data many times. Another example might be a `DateString` branded type that has been validated to have a certain format. More on reddit.com
๐ŸŒ r/typescript
26
31
December 20, 2023
NgFor Select list with null option
Wouldn't something like this work? Please Select {{ contact.name }} More on reddit.com
๐ŸŒ r/Angular2
5
3
January 19, 2016
๐ŸŒ
SPGuides
spguides.com โ€บ check-if-string-is-empty-typescript
How to Check if a String is Empty in TypeScript?
March 26, 2025 - To check if a string is empty in TypeScript, you can use the length property by verifying if str.length === 0, or use strict equality by comparing the string directly to an empty string with str === "". Additionally, to handle strings that contain ...
๐ŸŒ
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 - Sometimes, a string might contain whitespace characters that make it appear non-empty even when it is. In such cases, we can use the trim method to remove any leading or trailing whitespace characters before checking for emptiness. Here's an example: let str = " "; if (str.trim().length === 0) { console.log("The string is empty"); } else { console.log("The string is not empty"); }
๐ŸŒ
CoreUI
coreui.io โ€บ answers โ€บ how-to-check-if-a-string-is-empty-in-javascript
How to check if a string is empty in JavaScript ยท CoreUI
September 24, 2025 - The length property returns the number of characters in a string, so comparing text.length === 0 determines if the string contains no characters. In this example, an empty string '' has a length of 0, making the condition true.
๐ŸŒ
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 - To ensure our application is robust and free from unexpected errors, it's important to handle cases where a string might be undefined or null. 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. let myString; if (!myString) { console.log("The string is empty, undefined, or null"); } else { console.log("The string has content"); }
๐ŸŒ
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 - However, by applying the trim() method, we ensure that whitespace is eliminated before checking the length, and correctly identifying an empty string. It's important to note that the length property does not work for null or undefined values, ...
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
The first operand uses the typeof operator to check if the argument value is a string. If the value is a string, leading and trailing white space and line terminator strings are removed using the trim() method.
๐ŸŒ
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 - // Function to check string is empty or not function checking(str){ // Checking the string using === operator if (str === "") { console.log("Empty String") } else { console.log("Not Empty String") } } // Checking for empty string checking(""); ...
๐ŸŒ
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 (โ€œโ€).
๐ŸŒ
Medium
medium.com โ€บ @python-javascript-php-html-css โ€บ validating-empty-undefined-or-null-strings-in-javascript-fe483c3340ad
JavaScript Validation of Null, Undefined, and Empty Strings
August 24, 2024 - Yes, an empty string has a length of 0, while a string with spaces has a length corresponding to the number of spaces. Use string.trim().length === 0 to check for both. How do I check for both null and undefined in a single condition?
๐ŸŒ
Coding Beauty
codingbeautydev.com โ€บ home โ€บ posts โ€บ how to check if a string is empty in javascript
How to Check if a String is Empty in JavaScript - Coding Beauty
July 18, 2022 - Depending on your scenario, you might want to consider that the string could be a nullish value (null or undefined). To check for this, use the string if statement directly, like this:
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ typescript โ€บ how-to-check-if-an-object-is-empty-in-typescript
How to Check if an Object is Empty in TypeScript ? - GeeksforGeeks
July 23, 2025 - Converting the object into a JSON string allows for easy checking if it's empty by verifying the length of the resulting string. Example: This example shows the use of the above-explained approach. ... const obj7: Record<string, any> = {}; const obj8: Record<string, any> = { language: "TypeScript", version: "4.5" }; if (JSON.stringify(obj7) === '{}') { console.log('obj7 is empty'); } else { console.log('obj7 is not empty'); } if (JSON.stringify(obj8) === '{}') { console.log('obj8 is empty'); } else { console.log('obj8 is not 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.

๐ŸŒ
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:
๐ŸŒ
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 - This truthy check requires at least one character in the string to succeed. It evaluates to true if the value is not '' (empty string), undefined, null, false, 0, or NaN values.
๐ŸŒ
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 - The above snippet will print "This is empty, i.e. either null or undefined". The above code works for both null,undefined and empty string like "". Next, we are going to check if string is blank or having some white spaces.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-check-null-and-undefined-in-typescript
How to check null and undefined in TypeScript ? - GeeksforGeeks
August 1, 2024 - We can use typeof or '==' or '===' to check if a variable is null or undefined in typescript. By using typescript compiler tcs we transpile typescript code to javascript and then run the javascript file.
๐ŸŒ
Reddit
reddit.com โ€บ r/typescript โ€บ how to create a type that rejects empty string?
r/typescript on Reddit: How to Create a Type That Rejects Empty String?
December 20, 2023 -

I would like to create a type where it accepts all string except empty string. So buttonAriaLabel = "I am button" is acceptable, but if empty string is passed in buttonAriaLabel = " ", this will generate type error like "empty string is not assignable to type..." . I tried following technique (s: string): s is Excluse<typeof s, ' '> but it doesn't work at all. What are the techniques available to achieve this?

type NonEmptyString = {
  (s: string): s is Exclude<typeof s, ''>;
};
export interface IButton {
  // ... other properties
  buttonAriaLabel: NonEmptyString;
}

// Desired Result
const button: IButton = {
  // ... other properties
  buttonAriaLabel: 'Close', // Valid non-empty string
};
const invalidButton: IButton = {
  // ... other properties
  buttonAriaLabel: '' ", // TypeScript error: Type " " is not assignable to type 'NonEmptyString'.
};

Top answer
1 of 9
44
Even if you can find a way to enforce this with the type system, I would strongly recommend against this. This is a runtime check on the content and not a compile-time one- An empty string is still a string and this is extremely unusual. Youโ€™re guaranteed to create confusion down the line. Edit: Clarification- if this is your own personal project- have at it and have fun! If this is for a professional project with other developers, or will be handed off to other developers, this has a high potential for misuse and confusion.
2 of 9
17
IMO, the best way to do this is to use a "branded type" with a constructor function that validates at runtime. This means that anywhere you want to make a NonEmptyString type, you'll have to handle the potential error. declare const opaqueSym: unique symbol; export type NonEmptyString = string & { [opaqueSym]: "NonEmptyString" }; export function NonEmptyString(s: string): NonEmptyString { if (s.length === 0) { throw new Error("NonEmptyString: received an empty string") } return s as NonEmptyString } You could also do a custom type guard: export function isNonEmptyString(s: string): s is NonEmptyString { return s.length > 0; } All that said, this is probably way more effort than necessary when you can just check length at runtime, unless you're composing a bunch of functions that depend on the length being greater than zero. ETA: You can see how a library like Zod can be used to achieve this as well: https://zod.dev/?id=brand And just to restate that last line: the main benefit of using types like this is to make a promise to consuming code that some validation has taken place on a piece of data. It can be useful if you need to mark something as "validated" that might be consumed in many places, and/or if you are validating the same kind of data many times. Another example might be a `DateString` branded type that has been validated to have a certain format.