check an empty string in typescript

To check an empty string in typescript, 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.

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

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

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
let str: string = "";

// ✅ check an empty string in typescript
if(str){
  console.log("str is not empty");
} else {
  console.log("str is empty");
}

// 👉️ str is empty

In the above typescript example, we have taken the sample empty string variable, use the if..else statement, and print the output in the console.

Note: To bypass spaces in the string using the trim() method, it will remove both sides extra spaces from the string.

I hope it helps you, All the best 👍.