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
1const person = undefined;23if(person){4 console.log("person available")5} else {6 console.log("person undefined")7}
When you run the program, you will get the output person undefined
.

Handle null
example
1const person = null;23if(person){4 console.log("person available")5} else {6 console.log("person null")7}
When you run the above program, you will get the output person null
.

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.