To check if a string is empty in TypeScript, compare it with an empty string: str === "" (or str.length === 0). If the value can also be null or undefined, use str == null || str === "", and add trim() when a string of spaces should count as empty. This guide covers each case with a reusable isNullOrEmpty function, type narrowing with !str (and when it goes wrong), why there is no String.Empty in TypeScript, default values with ?? vs ||, and removing empty strings from an array.
Every example was compiled with TypeScript 5.2.2 and Node.js v22.22.2 in the Windows Command Prompt, using tsc --target es2022 --lib es2023,dom --strict file.ts and node file.js. Reference: Truthiness narrowing in the TypeScript Handbook and String.prototype.trim() on MDN.
Check if a string is empty
const firstName: string = "";
const lastName: string = "Miller";
console.log(firstName === ""); // true -> empty
console.log(firstName.length === 0); // true -> same check
console.log(lastName === ""); // false
Output:
true
true
false
=== "" and length === 0 give the same answer for a string.Both checks are fine when the variable is typed as string. Strict equality is the most readable, and TypeScript warns you if you compare a non-string with "" in many cases.
Check if a string is null or empty (isNullOrEmpty)
TypeScript has no built-in isNullOrEmpty like C#, but it is a two-line function. value == null (loose equality) is true for both null and undefined. A second function treats whitespace-only strings as empty too:
function isNullOrEmpty(value: string | null | undefined): value is "" | null | undefined {
return value == null || value === ""; // == null catches null and undefined
}
function isNullOrWhitespace(value: string | null | undefined): boolean {
return value == null || value.trim() === "";
}
const inputs: (string | null | undefined)[] = ["", " ", "\t\n", "Denver", null, undefined];
console.log("value".padEnd(12), "isNullOrEmpty".padEnd(15), "isNullOrWhitespace");
for (const v of inputs) {
console.log((v == null ? String(v) : JSON.stringify(v)).padEnd(12), String(isNullOrEmpty(v)).padEnd(15), isNullOrWhitespace(v));
}
Output:
value isNullOrEmpty isNullOrWhitespace
"" true true
" " false true
"\t\n" false true
"Denver" false false
null true true
undefined true true
isNullOrWhitespace.The return type value is "" | null | undefined is a type guard: after if (!isNullOrEmpty(s)), TypeScript treats s as a non-empty string.
Check for an empty string with !str
An empty string, null and undefined are all falsy, so if (!name) handles all three at once and narrows the type to string afterwards:
function greet(name?: string | null): string {
if (!name) { // "", null and undefined are all falsy
return "Hello, guest";
}
return `Hello, ${name.toUpperCase()}`; // TypeScript knows name is a string here
}
console.log(greet("sara"));
console.log(greet(""));
console.log(greet(null));
console.log(greet());
Output:
Hello, SARA
Hello, guest
Hello, guest
Hello, guest
When !value gives the wrong answer
Truthiness is only safe when the type is a string (or null/undefined). If a value can be a number, 0 is falsy too:
function describe(value: string | number | null) {
return !value ? "empty" : "has a value";
}
console.log("0 ->", describe(0)); // a real number, but !0 is true
console.log('"0" ->', describe("0")); // a non-empty string is truthy
console.log('"" ->', describe(""));
console.log("null ->", describe(null));
const isEmptyString = (v: unknown): v is "" => typeof v === "string" && v.length === 0;
console.log("strict check on 0:", isEmptyString(0), "| on \"\":", isEmptyString(""));
Output:
0 -> empty
"0" -> has a value
"" -> empty
null -> empty
strict check on 0: false | on "": true
Is there a String.Empty in TypeScript?
No. Code copied from C# fails to compile:
const title: string = String.Empty;
tsc output:
string_empty.ts(1,30): error TS2339: Property 'Empty' does not exist on type 'StringConstructor'.
String.Empty does not exist in TypeScript or JavaScript.Use the literal "", or define your own constant if your team prefers a name:
const EMPTY = "" as const; // TypeScript has no String.Empty; use "" or your own constant
let city: string = EMPTY;
console.log(JSON.stringify(city), city === "", city.length);
Output:
"" true 0
Default values for empty strings: ?? vs ||
The nullish coalescing operator ?? replaces only null and undefined. The || operator also replaces the empty string. Pick the one that matches what “empty” means for your data:
const nickname = "";
const middleName: string | null = null;
console.log(JSON.stringify(nickname ?? "N/A")); // ?? only replaces null/undefined -> ""
console.log(JSON.stringify(nickname || "N/A")); // || also replaces "" -> "N/A"
console.log(JSON.stringify(middleName ?? "N/A"));
console.log(nickname.trim() === "" ? "nickname is empty" : "nickname is set");
Output:
""
"N/A"
"N/A"
nickname is empty
"" ?? "N/A" keeps the empty string; "" || "N/A" does not.Check that a string is not empty: filter an array
To keep only non-empty strings, filter with a type predicate so the result is typed as string[]:
const rawTags: (string | null | undefined)[] = ["react", "", " ", null, "node", undefined, "typescript"];
const tags = rawTags.filter((t): t is string => t != null && t.trim() !== "");
console.log(tags); // string[] with no empty values
console.log(tags.length);
Output:
[ 'react', 'node', 'typescript' ]
3
| Goal | Check |
|---|---|
| Empty string | s === "" or s.length === 0 |
| Null, undefined or empty | s == null || s === "" or !s (string types only) |
| Blank (only spaces) | s.trim() === "" |
| Not empty | s !== "" or s.trim().length > 0 |
| Default for missing value | s ?? "N/A" (keeps “”) or s || "N/A" (replaces “”) |
Related TypeScript guides:
- Check if an object is empty in TypeScript
- Check if an array is null or empty
- Remove empty strings from an array in TypeScript
- Check the type of a variable in TypeScript
Frequently asked questions
How do I check if a string is empty in TypeScript?
Use str === "" or str.length === 0.
How do I check if a string is null or empty?
str == null || str === "". For string-typed values, !str covers the same three cases.
Does TypeScript have isNullOrEmpty?
No. Write a small function: const isNullOrEmpty = (s?: string | null) => s == null || s === "".
Is there String.Empty in TypeScript?
No, String.Empty is a compile error (TS2339). Use "".
How do I treat a string with only spaces as empty?
Trim it first: str.trim() === "".
Should I use ?? or || for a default string value?
?? keeps an empty string and only replaces null/undefined; || also replaces the empty string.
Bijay Kumar is a 13-time Microsoft MVP with more than 18 years in software development, and the founder of Python Guides and TSinfo Technologies. He started out building .NET and SharePoint solutions at HP, TCS and KPIT before moving into Python, machine learning and AI, and he also builds web apps with TypeScript and React. He writes the tutorials here himself, and every example is run before publishing so you see the real output. More about Bijay · Microsoft MVP profile · LinkedIn