Check If String Is Empty in JavaScript

Code examples to check an empty string in JavaScript

Use strict equality to check if string is empty when the input is already known to be a string: value === ”. To check if a string is empty in a validation rule, choose a different test when spaces should count as no content or when the value may be missing.

The practical answer to how to check if a string is empty depends on the definition of “empty”: an exact zero-character value, whitespace-only text, or an absent value.

Check if string is empty: value === ” or value.length === 0

For a value guaranteed to be a primitive string, these two checks identify an exact empty string:

  • value === ” compares the value directly with JavaScript’s empty string literal. It does not coerce numbers, booleans, or other values.
  • value.length === 0 checks whether the string contains zero characters. It produces the same result for a known string.

Strict equality is usually the clearest option because it states the intended value directly. The length form is useful when you are already working with string properties or want to emphasize the character count.

Neither check treats a string containing spaces as empty. For example, ‘ ‘ === ” is false, and the string’s length is greater than zero.

Treat whitespace-only text as empty with value.trim().length === 0

Use value.trim().length === 0 when leading and trailing whitespace should not count as content. The trim() method removes whitespace from both ends of a string, so this condition matches all of these examples:

  • ”
  • ‘ ‘
  • ‘ ’
  • ‘ text ‘ does not match because text remains after trimming

trim() returns a new string and does not modify the original value. Use the exact equality or length check instead when spaces are meaningful, such as in a fixed-format value where padding must be preserved.

For checking an empty string in JavaScript when the input type is not guaranteed, combine trimming with a type guard:

typeof value === ‘string’ && value.trim().length === 0

This prevents trim() from being called on a number, object, or other non-string value.

Handle null or undefined before checking

null and undefined are missing-value states, not empty strings. They require a separate decision. Accessing value.length or calling value.trim() on either value throws a TypeError, so guard them before using string methods.

An explicit null or undefined guard is:

value === null || value === undefined

For an optional field where missing values should be rejected separately from empty text, use conditions such as:

  • value === null || value === undefined for a missing value
  • typeof value === ‘string’ && value.length === 0 for an exact empty string
  • typeof value === ‘string’ && value.trim().length === 0 for empty or whitespace-only text

If your rule defines both missing and blank as invalid, combine those cases explicitly:

value === null || value === undefined || (typeof value === ‘string’ && value.trim().length === 0)

This condition does not silently classify unrelated types as empty. The shorter expression value == null also matches only null and undefined, but the explicit form makes that intent easier to read.

Avoid truthiness surprises with !value and optional values

!value tests whether a value is falsy, not whether it is an empty string. It evaluates to true for an empty string, but also for:

  • 0
  • false
  • null
  • undefined
  • NaN

It also does not identify whitespace-only text: !’ ‘ is false because a non-empty string is truthy. Use !value only when every falsy value should be treated as invalid. If zero or false are valid inputs, use a strict string, trimming, or null check that matches the required definition of empty.