Hi Friends đź‘‹,

Welcome To Infinitbility! ❤️

To check a variable is undefined in typeScript, use if...else statement it will handle all falsy value like undefined, null, empty and etc.

Let’s see short example to use if...else with variableName to check a variable is undefined in typeScript.

if(variableName){

}

Today, I’m going to show you How do I check a variable is undefined in typeScript, as above mentioned, I’m going to use the above-mentioned if...else statement.

Let’s start today’s tutorial how do you check a variable is undefined in typeScript?

TypeScript check a variable is undefined example

Here, we will take string variable with some data and use if...else statement to check a variable is undefined.

let string: string | undefined;

if(string){
  console.log("string is defined")
} else {
  console.log("string is undefined")
}

// You can also check specific conditions like a string is undefined or not

if(string == undefined){
  console.log("string is undefined")
} else {
  console.log("string is defined")
}

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