How to check if string is empty in typescript

In this Typescript tutorial, we will see how to check if a string is empty in typescript. We are going to cover below topics:

  • How to check if the string is empty typescript
  • How to check if the string is empty or whitespace typescript
  • How to check if the string is empty or undefined typescript
  • How typescript check if the string is null or whitespace

Check if string is empty typescript

Here we will see how to check if the string is empty or not in the typescript.

For example, we will use the typescript the length property to check whether the string is empty or not, e.g. ‘if(str.length===0){}’. Here in this example, the length of the string is 0, then it is empty, else it is not empty.

Method: 1

In the code editor, create a file with the name ‘checkStringIsEmpty.ts’, then write the below code:

let text = '';

if (typeof text === 'string' && text.length === 0) {
  console.log('string is empty');
} else {
  console.log('string is NOT empty') // 👉outputs: string is empty
}

To compile the code, run the below command and you can see the result in the console.

ts-node checkStringIsEmpty
Check if the string is empty typescript
Check if the string is empty typescript

This is an example to check if the string is empty in typescript.

How to check if the string is empty or whitespace typescript

Here we will see how to check if the string is empty or whitespace in the typescript

In the checkStringIsEmpty.ts file write the below code, which will trim the space from the empty string using the trim method, if the string is empty.

let text = '  ';

if (typeof text === 'string' && text.trim().length === 0) {
  console.log('string is empty');
} else {
  console.log('string is not empty') // 👉outputs: string is empty
}

To compile the code and run the below command and you can see the result in the console.

ts-node checkStringIsEmpty
How to check if the string is empty or whitespace typescript
How to check if the string is empty or whitespace typescript

This is an example of how to check if the string is empty or white space typescript.

How to check if string is empty or undefined Typescript

Here we will see how to check if the string is empty or undefined in the typescript.

For example, we will create a function that will check whether the string is empty or undefined in typescript, bypassing the value to the parameter in typescript.

In the checkStringIsEmpty.ts file, write the below code:

function checkIfEmpty(strEntry:any) {
    if (strEntry) {
      console.log('string is NOT empty');
    } else {
      console.log('string is empty');
    }
  }
  
  const strEntry1 = 'not empty';
  const strEntry2 = ''; // empty
//   const strEntry3 = null;
  const strEntry4 = undefined;
  
  checkIfEmpty(strEntry1); // 👉outputs: string is NOT empty
  checkIfEmpty(strEntry2); // 👉outputs: string is empty

  checkIfEmpty(strEntry4); // 👉outputs: string is empty

To compile the code and run the below command and you can see the result in the console.

ts-node checkStringIsEmpty
How to check if the string is empty or undefined Typescript
How to check if the string is empty or undefined Typescript

This is an example of checking if the string is empty or undefined Typescript.

How does typescript check if the string is null or whitespace

Here we will see an example of how to check if the string is null or whitespace in typescript.

For example, we will create a function, that will check if the string variable contains null or whitespace, or empty or not empty. In the checkStringIsEmpty.ts file, write the below code:

function checkIsEmpty(strEntry) {
    if(strEntry===null){
        console.log('String is null')
    }
    else if (strEntry.trim().length === 0) {
      console.log('String is empty');
    } else {
      console.log('String is NOT empty');
    }
  }



  const strEntry1 = 'not empty';
  const strEntry2 = ''; // empty
  const strEntry3 = '   '; // contains only whitespace
  const strEntry4 = null;



  checkIsEmpty(strEntry1); //👉outputs: String is NOT empt
  checkIsEmpty(strEntry2); //👉outputs: String is empty
  checkIsEmpty(strEntry3); //👉 outputs: String is empty
  checkIsEmpty(strEntry4); //👉 outputs: String is null

To compile the code and run the below command and you can see the result in the console.

ts-node checkStringIsEmpty
How typescript check if the string is null or whitespace
How does typescript check if the string is null or whitespace

This is an example of check if the string is null or whitespace in typescript

Conclusion

In this typescript tutorial, we saw how we can check if the string is empty or null or whitespace using the length and trim method. So here are the topics we covered:

  • How to check if the string is empty typescript
  • How to check if the string is empty or whitespace typescript
  • How to check if the string is empty or undefined typescript
  • How typescript check if the string is null or whitespace

You may like the following typescript tutorials:

>