Navigate back to the homepage

How to check both undefined and null in typescript?

Infinitbility
TypeScript
May 10th, 2022 · 1 min read
keep affilitate

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;
2
3if(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.

TypeScript, Handle undefined example
TypeScript, Handle undefined example

Handle null example

1const person = null;
2
3if(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.

TypeScript, Handle null example
TypeScript, Handle null example

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.

melio affilitate

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to get last element of array in typescript?

TypeScript, get last element of array example with step by step guide.

May 9th, 2022 · 1 min read

How to get random number in typescript?

TypeScript, get random number example with step by step guide.

May 8th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]