How to check if a string is null, blank, empty or undefined using JavaScript?

Published on 2021-08-10, by Javed Shaikh

Subscribe for new article
*No spam. unsubscribe at anytime

In this post I am going to show how I normally do validation for empty string. We are going to check string with different values such as blank, undefined, null and white spaces. We will go one by one with examples.

Check if a string is null or undefined

To see if a string is null or undefined, we can simply use below snippet to check. Here we are using logical NOT operator to identify is a const variable str holds a string.

const str1 = null
const str2 = undefined

if (!str1 || !str2){
  console.log("This is empty!, i.e. either null or undefined")
}

The above snippet will print "This is empty, i.e. either null or undefined". The above code works for both null,undefined and empty string like "".

Check if a string has any white spaces

Next, we are going to check if string is blank or having some white spaces.

const str3 ="   "
if (!str3.trim()){
  console.log("Its not an empty string")
}

Here we are using JavaScript's trim() function to remove all the white spaces from both ends of the string and then check if its empty.

Write a function to check if a string is empty?

Lets combine both checks and include these in our own utility function so that we can use it repeatedly anywhere we need.

const isEmpty = (str) => !str || !str.trim();

In above snippet, we are using JavaScript's arrow function to minimize lines of code. Here we have two checks, the first one will check for all null, empty and undefined strings and the second check is for white space characters. The above function will return true if a string is empty or false if not.

Lets use some demo to check how this function works.

EXAMPLE:

//function to check if a string is empty
  const isEmpty = (str) => !str || !str.trim();
  
  const str1 = null;       //null
  const str2 = "";         //empty
  const str3 = undefined;  //undefined
  const str4 = "        "; //white space
  const str5 = 0;          //0
  const str6 = "shaikhu";  //shaikhu

  console.log(isEmpty(str1)); // true
  console.log(isEmpty(str2)); // true
  console.log(isEmpty(str3)); // true
  console.log(isEmpty(str4)); // true
  console.log(isEmpty(str5)); // true
  console.log(isEmpty(str6)); // false

My other recent posts

About the Author

I am a backend system engineer working for a credit card issuer. I mostly work on C/C++ and assembler programs on IBM's TPF OS. However I love web development and keep trying my hands on Nodejs and Python when offwork

Connect with author

Related articles ...

How to use localStorage and sessionStorage in JavaScript?

World Wide Web Consortium or W3C introduced two storage object mechanisms to store data in the browser which are localStorage and sessionStorage objects

2021-07-28

How to check if a string contains a substring or not in JavaScript?

There are two methods in JavaScript to find if a string contains another string or not. The includes() method returns true or false after a search and indexOf() method returns index of a string or -1 if it does not find a match

2021-08-08

What is http cookie and how it works?

The HTTP protocol is stateless which means that the server does not remember about any response it sent to the user’s browser. It executes each request independently without knowing past requests that are already executed.

2021-07-26