Hi Friends đź‘‹,

Welcome To Infinitbility! ❤️

To check an empty string in node js, use the if..else statement just pass and put your string variable in an if condition if it’s empty it will go to the else statement.

In the following example, we will take the sample empty string and use the if..else statement.

let str = "";

if(str){
  console.log("str is not empty");
} else {
  console.log("str is empty");
}

// str is empty

Today, I’m going to show you How do i check an empty string in node js, as mentioned above, I’m going to use the use if..else statement to check node js empty string.

Let’s start today’s tutorial how do you check an empty string in node js?

In this example, we will do

  1. take an example of an empty string variable
  2. use the if..else statement
  3. send the output in the response
var http = require("http");

//create a server object:
http
  .createServer(function (req, res) {
    let str = "";

    let output = "";
    if(str){
      output = "str is not empty";
    } else {
      output = "str is empty";
    }

    res.write(output);
    res.end(); //end the response
  })
  .listen(8080); //the server object listens on port 8080

In the above node js example, we have taken the sample empty string variable, use the if..else statement, and send the output in the response.

I hope it helps you, All the best đź‘Ť.