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.

๐ŸŒ
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 ...
๐ŸŒ
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.
๐ŸŒ
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 for an empty string, the logical && operator is used. 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 ...
๐ŸŒ
Dirask
dirask.com โ€บ posts โ€บ TypeScript-check-if-string-is-empty-null-undefined-pORRXD
TypeScript - check if string is empty / null / undefined
In this article, we would like to show you how to check if the string is empty / null / undefined in TypeScript. Quick solution: If you only want to check if a ...
๐ŸŒ
LinkedIn
linkedin.com โ€บ pulse โ€บ check-empty-string-typescript-hossein-mohammadi
Check Empty String in Typescript
June 5, 2022 - type AlphaChar | "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z"; type NumberChar = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"; type Symbols = | "_" | "-" | " " | "#" | "!" | "@" | "$" | "%" | "^" | "&" | "*" | "(" | ")" | "{" | "}" | "[" | "]" | ":" | ";" | "," | "?" | "~" | "`" | "|" | "=" | "+" | "\\" | "/" | "<" | ">" | "." | "," | "'" | '"'; type Character = | AlphaChar | Lowercase<AlphaChar> | NumberChar | Symbols; type NoEmptyString = `${Character}${string}`;
Find elsewhere
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ javascript-check-if-string-is-empty
How to check if a String is Empty in JavaScript | bobbyhadz
Use the `length` property on the string to check if it is empty. If the string's length is equal to `0`, then it's empty, otherwise, it isn't 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.
๐ŸŒ
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
To see if a string is null or undefined, we can simply use below snippet to check. Here we are using logical NOT operator to identify is a const variable str holds a string. ... 1const str1 = null 2const str2 = undefined 3 4if (!str1 || !str2){ 5 console.log("This is empty!, i.e. either null or undefined") 6} 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 "".
๐ŸŒ
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 - JavaScript has several ways to check whether a string is empty or null. Let's explore some of them. One way to check for an empty or null string is to use the if statement and the typeof operator.
๐ŸŒ
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 - For a more comprehensive check including null and undefined, use !text || text.length === 0. The strict comparison === 0 is preferred over == 0 to avoid type coercion issues and ensure precise empty string detection.
๐ŸŒ
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
In such cases, trim the string value before checking itโ€™s length. The trim method removes leading and trailing whitespaces from a string. If a string contains only whitespaces it returns an empty string:
๐ŸŒ
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 - An empty string in JavaScript is represented by "" (a string with no characters). To check if a string is empty, you can use the strict equality operator (===) or the length property of the string.
๐ŸŒ
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) { return (!str || str.length === 0 ); } console.log(checking("")); console.log(checking("GeeksforGeeks")); ... This approach uses replace() Method.
๐ŸŒ
W3docs
w3docs.com โ€บ javascript
How to Check for Empty/Undefined/Null String in JavaScript
Another option is checking the empty string with the comparison operator โ€œ===โ€. ... The JavaScript strings are generally applied for either storing or manipulating text. There is no separate for a single character.
๐ŸŒ
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 can use two major methods that are somewhat similar because we will use the strict equality operator (==). In this first method, we will check for the length of the string by adding the length property.
๐ŸŒ
Sling Academy
slingacademy.com โ€บ article โ€บ typescript-how-to-check-if-a-string-is-empty
TypeScript: How to Check if a String is Empty - Sling Academy
An empty string has a length of 0, providing a straightforward check for emptiness. In JavaScript and TypeScript, an empty string is falsy.
๐ŸŒ
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!"