Check if a String is Empty in JavaScript or Node.js

In web applications you may accept user inputs. The users fill out forms and submit them to send the values to your backend. Receiving data on your server usually comes with data validations. The validation steps could require a string value of a given length for a field. In other cases you may only ensure the value is not an empty string.

This tutorial walks you through the steps of detecting whether a given string value is empty.

Node.js Series Overview

  1. String Replace All Appearances
  2. Remove All Whitespace From a String in JavaScript
  3. Generate a Random ID or String in Node.js or JavaScript
  4. Remove Extra Spaces From a String in JavaScript or Node.js
  5. Remove Numbers From a String in JavaScript or Node.js
  6. Get the Part Before a Character in a String in JavaScript or Node.js
  7. Get the Part After a Character in a String in JavaScript or Node.js
  8. How to Check if a Value is a String in JavaScript or Node.js
  9. Check If a String Includes All Strings in JavaScript/Node.js/TypeScript
  10. Check if a Value is a String in JavaScript and Node.js
  11. Limit and Truncate a String to a Given Length in JavaScript and Node.js
  12. Split a String into a List of Characters in JavaScript and Node.js
  13. How to Generage a UUID in Node.js
  14. Reverse a String in JavaScript or Node.js
  15. Split a String into a List of Lines in JavaScript or Node.js
  16. Split a String into a List of Words in JavaScript or Node.js
  17. Detect if a String is in camelCase Format in Javascript or Node.js
  18. Check If a String Is in Lowercase in JavaScript or Node.js
  19. Check If a String is in Uppercase in JavaScript or Node.js
  20. Get the Part After First Occurrence in a String in JavaScript or Node.js
  21. Get the Part Before First Occurrence in a String in JavaScript or Node.js
  22. Get the Part Before Last Occurrence in a String in JavaScript or Node.js
  23. Get the Part After Last Occurrence in a String in JavaScript or Node.js
  24. How to Count Words in a File
  25. How to Shuffle the Characters of a String in JavaScript or Node.js
  26. Append Characters or Words to a String in JavaScript or Node.js
  27. Check if a String is Empty in JavaScript or Node.js
  28. Ensure a String Ends with a Given Character in JavaScript or Node.js
  29. Left-Trim Characters Off a String in JavaScript or Node.js
  30. Right-Trim Characters Off a String in JavaScript or Node.js
  31. Lowercase the First Character of a String in JavaScript or Node.js
  32. Uppercase the First Character of a String in JavaScript or Node.js
  33. Prepend Characters or Words to a String in JavaScript or Node.js
  34. Check if a String is a Number
  35. Convert a String to Buffer
  36. Prevent Line Breaks in String Template Literals
  37. How to Implement a Custom `toString` Method
  38. What Is `Symbol.toStringTag` and How to Use It (Coming soon)

Detect if a String Value is Empty Using JavaScript

The first step in determining if a String is empty is to make sure the given value is actually a string. Please have a look at this tutorial on checking if a value is a string here at Future Studio.

Then, you may check if the string has a length greater than zero. If the string length is equal to zero it’s empty:

const string = ''

if (string.length === 0) {  
  // empty
}

The length check doesn’t catch string values consisting of spaces only. Sometimes you want to detect a string out of whitespaces as an empty string. 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:

const string = '   '

if (string.trim().length === 0) {  
  // empty
}

Another option is to check whether the string value is actually truthy:

if (string) {  
  // not empty
}

This truthy check requires at least one character in the string to succeed. It evaluates to true if the value is not '' (empty string), undefined, null, false, 0, or NaN values.

Use the @supercharge/strings Package

I’m the maintainer of the @supercharge/strings package providing convenient string utilities. The @supercharge/strings package comes with a handy Str#isEmpty method.

This isEmpty method determines whether the wrapped value is an empty string.

Please notice: the @supercharge/strings package wraps a given value. It creates an empty string from null and undefined when wrapping these values.

Here are example on how you can use this package to detect whether a string is empty or not:

import { Str } from '@supercharge/string'

Str().isEmpty()  
Str('').isEmpty()  
Str(null).isEmpty()  
Str('  ').trim().isEmpty()  
// true

Str('  ').isEmpty()  
Str('Future Studio').isEmpty()  
// false

You can also check the opposite direction using the isNotEmpty method:

import { Str } from '@supercharge/string'

Str().isNotEmpty()  
Str('').isNotEmpty()  
Str(null).isNotEmpty()  
Str('  ').trim().isNotEmpty()  
// false

Str('  ').isNotEmpty()  
Str('Future Studio').isNotEmpty()  
// true

Enjoy detecting empty strings!


Mentioned Resources

Explore the Library

Find interesting tutorials and solutions for your problems.