DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk2 min

How to Check if a String Is Empty in TypeScript

Check exact emptiness with value.length === 0 or value === "". Use trim() only when whitespace-only strings should count as blank, and guard nullable values first.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For an exact empty-string check, use value.length === 0 or value === "". If whitespace-only text should count as blank, use value.trim().length === 0 instead. For values that may be null or undefined, check those cases before accessing string properties.

Check for an exactly empty string

An empty string has zero characters. Both of these checks express that rule:

const isEmpty = (value: string): boolean => value.length === 0;
const isEmptyByLiteral = (value: string): boolean => value === "";

The first checks the string’s length; the second compares it directly with the empty-string literal. Choose whichever makes the intent clearest to your team. Neither treats a string containing spaces as empty.

Decide whether whitespace counts as blank

If your validation rule considers whitespace-only input blank, trim the string before checking its length:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const isBlank = (value: string): boolean => value.trim().length === 0;

This is a different rule from exact emptiness: " " has characters, but its trimmed value has none. Use it only when that matches the behavior you want, such as rejecting a form field containing only whitespace.

Handle null or undefined before using string methods

A value typed as string | null | undefined may not have a string’s length property. With strictNullChecks, TypeScript treats null and undefined as distinct types, so narrow the union first. For example, to treat either nullish value or an empty string as absent:

Rank #2
TypeScript Programming Language - Software Engineer & Coder T-Shirt
  • TypeScript implements a superset of syntax for strictly typed development, facilitating deep static analysis and enhanced development environment integration. The compiler translates source into standard script formats, ensuring parity across any runtime.
  • TypeScript is ideal for front-end developers, full-stack engineers, and software architects who build large-scale web applications. It serves those looking to improve code excellence, reduce bugs through static checking, and maintain complex projects more.
  • Lightweight, Classic fit, Double-needle sleeve and bottom hem
function isAbsentOrEmpty(value: string | null | undefined): boolean {
  return value == null || value.length === 0;
}

In JavaScript and TypeScript, value == null matches both null and undefined. The right side of the || is evaluated only when the first condition is false, so length is read only after the nullish cases have been excluded. If you want to preserve an empty string while handling null separately, compare explicitly:

if (value !== null) {
  // value is not null here; it may still be an empty string
}

Equality checks can narrow union types in TypeScript’s control flow. See the TypeScript Handbook’s narrowing guide and the TSConfig Reference entry for strictNullChecks.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Why a truthiness check is not the same

In JavaScript, "" is falsy. That makes if (!value) a possible shorthand when value is known to be a string and the only distinction you care about is empty versus non-empty. But truthiness is broader than an explicitly named empty-string test: if the type later includes other falsy values, the condition catches those too.

It can also accidentally skip work that should happen for an empty string. The TypeScript Handbook warns that wrapping a function body in a truthiness check can mean “we may no longer be handling the empty string case correctly.” Use an explicit length or literal comparison when that case needs deliberate handling.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose the check that matches the input rule

Check Meaning Use when
value.length === 0 Exactly zero characters value is a string and whitespace is meaningful.
value === "" Equal to the empty-string literal You want an explicit exact-empty comparison.
value.trim().length === 0 Empty after trimming whitespace Your rule treats whitespace-only text as blank.
value == null || value.length === 0 Null, undefined, or exactly empty The input type allows nullish values and all should count as absent.
if (value) or if (!value) Truthiness or falsiness, not specifically string length Only when the broader truthiness behavior is intentional.

These forms are alternatives for different semantics, not performance claims; the cited documentation does not establish a speed advantage for one check.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.