;

How to check if string empty or undefined or null in JavaScript


Tutorialsrack 12/03/2020 Jquery Javascript

In this article, we will learn how to check if the string is empty or undefined or null in javascript.

There are many ways to check if the string is empty or undefined or null in javascript.

Example 1: To Check if String is Empty or Undefined or Null

Example 1
// To Check if String is Empty or Undefined or Null
if (str) {
    //Do Something Here
    alert(true)
}else{
  alert('If Null/Empty/Undefined: false')
}
 
//***Test cases and results***//
 
// str = ''; // it returns false
// str = 0; // it returns false
// str = 7; // it returns true
// str = null; // it returns false
// str; // it returns false
// str = undefined // it returns false
// str = '' // it returns false
// str = ' ' // it returns true
// str = 'tutorialsrack' // it returns true

Example 2: In this example, this isEmptyorBlankorNullorUndefined() function returns true when the string is empty or undefined or null or 0, otherwise it returns false.

Example 2
function isEmptyorBlankorNullorUndefined(str) {
    return (!str || 0 === str.length);
}
 
//***Test cases and results***//
 
// str = ''; // it returns true
// str = 0; // it returns true
// str = 7; // it returns false
// str = null; // it returns true
// str; // it returns true
// str = undefined // it returns true
// str = ' ' // it returns false
// str = 'tutorialsrack' // it returns false

Example 3: In this example, this isEmptyorBlankorNullorUndefined() function returns true when the string is empty or undefined or null, otherwise it returns false.

Example 3
function isEmptyorBlankorNullorUndefined(str) {
    return (!str || /^\s*$/.test(str));
}
 
//***Test cases and results***//
 
// str = ''; // it returns true
// str = 0; // it returns true
// str = 7; // it returns false
// str = null; // it returns true
// str; // it returns true
// str = undefined // it returns true
// str = ' ' // it returns true
// str = 'tutorialsrack' // it returns false

Example 4: In this example, using !!str

Example 4
// To Check if String is Empty or Undefined or Null
 
if(!!str){
    // some code here
    alert(true);
}else{
  alert('If Null/Empty/Undefined: false');
}
 
//***Test cases and results***//
 
// str = ''; // it returns false
// str = 0; // it returns false
// str = 7; // it returns true
// str = null; // it returns false
// str; // it returns false
// str = undefined // it returns false
// str = ' ' // it returns true
// str = 'tutorialsrack' // it returns true

Example 5: in this example, we use type casting

Example 5
if(Boolean(str)){
    // code here
    alert(true);
}else{
    alert('If Null/Empty/Undefined: false');
}
 
//***Test cases and results***//
 
// str = ''; // it returns false
// str = 0; // it returns false
// str = 7; // it returns true
// str = null; // it returns false
// str; // it returns false
// str = undefined // it returns false
// str = ' ' // it returns true
// str = 'tutorialsrack' // it returns true

For example 4 and 5, both do the same function.

Both Returns false for null,undefined,0,000,"",false.

Both Returns true for string "0" and whitespace " ".

Example 6: In this example, we can have one function that checks for all “empties” like null, undefined, '', ' ', {}, [] 

Example 6
var isEmptyorBlankorNullorUndefined = function(data) {
    if(typeof(data) === 'object'){
        if(JSON.stringify(data) === '{}' || JSON.stringify(data) === '[]'){
            return true;
        }else if(!data){
            return true;
        }
        return false;
    }else if(typeof(data) === 'string'){
        if(!data.trim()){
            return true;
        }
        return false;
    }else if(typeof(data) === 'undefined'){
        return true;
    }else{
        return false;
    }
}


//***Test cases and results***//

console.log(isEmpty()); // it returns true 
console.log(isEmpty(null)); // it returns true 
console.log(isEmpty('')); // it returns true 
console.log(isEmpty(' ')); // it returns true 
console.log(isEmpty(undefined)); // it returns true 
console.log(isEmpty({})); // it returns true 
console.log(isEmpty([])); // it returns true 
console.log(isEmpty(0)); // it returns false 
console.log(isEmpty('Hey')); // it returns false

I hope this article will help you to understand how to check if the string is empty or undefined or null in javascript.

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags