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.

javascript
1const str1 = null 2const str2 = undefined 3 4if (!str1 || !str2){ 5 console.log("This is empty!, i.e. either null or undefined") 6}

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.

javascript
1const str3 =" " 2if (!str3.trim()){ 3 console.log("Its not an empty string") 4}

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.

javascript
1const 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:

javascript
1 //function to check if a string is empty 2 const isEmpty = (str) => !str || !str.trim(); 3 4 const str1 = null; //null 5 const str2 = ""; //empty 6 const str3 = undefined; //undefined 7 const str4 = " "; //white space 8 const str5 = 0; //0 9 const str6 = "shaikhu"; //shaikhu 10 11 console.log(isEmpty(str1)); // true 12 console.log(isEmpty(str2)); // true 13 console.log(isEmpty(str3)); // true 14 console.log(isEmpty(str4)); // true 15 console.log(isEmpty(str5)); // true 16 console.log(isEmpty(str6)); // false

My other recent posts

About the Author

I am a Backend System Engineer at a credit card company, specializing in C/C++ and assembler on IBM's TPF OS. I have a passion for web development and enjoy working with Node.js and Python in my free time.

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