Hi Friends đź‘‹,

Welcome to Infinitbility ❤️!

Today, we are going to learn how we can check both undefined and null in typescript, here we will use the check falsy value concept to check both undefined and null.

To handle, false, '', undefined, null, and other false value we can easily manage in single default condition in typescript.

Here, we have to just check variables in if cases.

Well, we will take an example of undefined, and null. let’s see how we can do it.

let’s start with a check undefined example.

Handle undefined example

const person = undefined;

if(person){
  console.log("person available")
} else {
  console.log("person undefined")
}

When you run the program, you will get the output person undefined.

TypeScript, Handle undefined example
TypeScript, Handle undefined example

Handle null example

const person = null;

if(person){
  console.log("person available")
} else {
  console.log("person null")
}

When you run the above program, you will get the output person null.

TypeScript, Handle null example
TypeScript, Handle null example

All the best đź‘Ť.