Skip to main content

How to Check if a String Is Empty in JavaScript

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

A value your program needs, such as a setting or a form field, can be missing in more than one way: an empty string '', undefined or null. In JavaScript one test catches all three, because all three are falsy.

Syntax​

Syntax
if (!<value>) {
// <value> is '', undefined, null or another falsy value
}
PartWhat it does
!<value>The logical NOT operator: true when <value> is falsy, false when it's truthy.
if (...)Runs the block when the condition is true.

MDN's falsy reference: "A falsy (sometimes written falsey) value is a value that is considered false when encountered in a Boolean context." The falsy values include '', undefined, null, 0 and false.

The Example​

The example runs four values through one check: a name, an empty string, undefined and null.

Prerequisites​

  • Node.js. This guide uses Node.js 26.13.1.

Step 1: Write the Check​

Create a file named main.js:

main.js, which defines describe(value) returning empty or missing when !value is true and has text: followed by the value otherwise, and prints it for Ada, an empty string, undefined and null.

main.js
function describe(value) {
if (!value) {
return 'empty or missing';
}
return `has text: ${value}`;
}

console.log(describe('Ada'));
console.log(describe(''));
console.log(describe(undefined));
console.log(describe(null));

Step 2: Run It​

Terminal
node main.js
Output
has text: Ada
empty or missing
empty or missing
empty or missing

A zsh terminal where node main.js prints has text: Ada, then empty or missing three times.

!value was true for the empty string, undefined and null, so one if handled all three. The command is the same in zsh, bash and PowerShell.

What About Spaces?​

A string of spaces, such as ' ', isn't empty, so it's truthy and passes the check. When spaces should count as empty too, trim them first:

Blank strings count as empty
if (!value?.trim()) {
// '', ' ', undefined and null all land here
}

?. stops at undefined or null instead of failing, and trim() turns ' ' into ''. I ran 'Ada', '', ' ', undefined and null through !value?.trim(): only 'Ada' gave false.

Common Mistakes​

MistakeWhat happensDo this instead
Using if (!value) for numbers0 is falsy too, so a real 0 counts as missingTest numbers with value === undefined
Expecting ' ' to count as emptyA string of spaces is truthyUse !value?.trim()
Calling value.trim() when value may be missingTypeError: Cannot read properties of undefined (reading 'trim')Use value?.trim()

Troubleshooting​

SymptomLikely causeFix
A blank form field slips throughIt holds spacesTrim before testing
A 0 is treated as missing!0 is trueCompare with undefined or null instead

FAQ​

Is the string '0' falsy?​

No. Only the empty string is a falsy string; '0', 'false' and ' ' are all truthy.

How do I check only for an empty string?​

Compare it directly: value === '' is true only for the empty string.

Conclusion​

if (!value) catches '', undefined and null in one test, because all three are falsy. Add ?.trim() when strings of spaces should count as empty too.

Additional Resources​