Skip to content

JavaScript if not empty | Example code

  • by

Use the Strict equality (===) operator in the if statement to check if not empty given string or variable in JavaScript. This is because === will only return true if the values on both sides are of the same type, in this case, a string.

if (variable === "") {
    // Code
}

JavaScript if not empty

Simple example code where the function is used to determine if the string is empty or not. If blank, the function will print The string is empty else, it will print The string is not empty.

<!DOCTYPE html>
<html>
<body>
  <script>

    function isEmptyCheck(str) {
      if (str === "") {
        console.log("The string is empty")
      }
      else{
        console.log("The string is not empty") 
      }
    }
    isEmptyCheck("") 
    isEmptyCheck("Hello Javascript")
  </script>
</body>
</html>

Output:

JavaScript if not empty

Check if the string is empty using the length and ! operator

function isEmptyFunction(str) {
    return (!str|| str.length === 0 );
}
console.log(isEmptyFunction(""))
console.log(isEmptyFunction("Hello Javascript"))

Check if the string is empty or has only white spaces

function isEmptyFunc(str) {
    return (str.length === 0 || !str.trim());
};

console.log(isEmptyFunc("Hello Javascript")) // false
console.log(isEmptyFunc(""))
 // false
console.log(isEmptyFunc("    ")) // true

In JavaScript, you can check if a variable is not empty or has a value using various methods depending on the data type and what you mean by “empty”. Here are some common scenarios:

Strings

var str = "Hello";

if (str) {
    // String is not empty
    console.log("String is not empty");
} else {
    // String is empty or falsy
    console.log("String is empty");
}

Arrays

var arr = [1, 2, 3];

if (arr.length > 0) {
    // Array is not empty
    console.log("Array is not empty");
} else {
    // Array is empty
    console.log("Array is empty");
}

Objects

var obj = { name: "John", age: 30 };

if (Object.keys(obj).length > 0) {
    // Object is not empty
    console.log("Object is not empty");
} else {
    // Object is empty
    console.log("Object is empty");
}

For checking if a variable is defined:

var x;

if (typeof x !== 'undefined') {
    // Variable is defined and not empty
    console.log("Variable is defined and not empty");
} else {
    // Variable is undefined or empty
    console.log("Variable is undefined or empty");
}

For checking null or undefined:

var variable;

if (variable !== null && variable !== undefined) {
    // Variable is not null or undefined
    console.log("Variable is not null or undefined");
} else {
    // Variable is null or undefined
    console.log("Variable is null or undefined");
}

Do comment if you have any doubts or suggestions on this Js if statement topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading