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
- take an example of an empty string variable
- use the
if..else
statement - send the output in the response
1let str: string = "";23// ✅ check an empty string in typescript4if(str){5 console.log("str is not empty");6} else {7 console.log("str is empty");8}910// 👉️ 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 👍.
Follow me on Twitter
Join our email list and get notified about new content
No worries, I respect your privacy and I will never abuse your email.
Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.