Check For Undefined In TypeScript

Check for undefined in TypeScript

This article will show you how to check for undefined in TypeScript with coding examples in different cases. This will give you knowledge about checking for undefined and help you distinguish between undefined and null.

Null and Undefined in TypeScript Programming

The value undefined is a variable that has been declared but has not been assigned a value.

A null value is a variable with no value.

How to Check For Undefined in Typescript?

We will learn to check for undefined in TypeScript with coding examples in different scenarios.

Using “===”

In TypeScript, using “===” to validate a variable helps the programmer check its value and type value.

let userName:string|undefined;
userName = "LearnShareIT"

if(userName===undefined){
    alert(' User Name is undefined ');
}
else{
    alert(` User Name is ${userName} `);
}

In the first line, we declare the variable userName with an unknown data type or string. Then the variable will be checked in the if. “===” allows you to check the value and type value of the variable userName. If userName has a string value, the output will look like this:

Conversely, if the userName variable is not assigned a value, the output will display as follows:

Using “==”

In TypeScript one uses “==” in the case of just checking the value.

let userName:string|undefined;

if(userName==undefined){
    alert(' User Name is undefined ');    
}
else{
    alert(` User Name is ${userName} `);
}

Check for Undefined by checking for Null

Another way to check for undefined in TypeScript, you can use null to replace undefined in the if condition. Because if you use “==”, the if condition will return true if the variable has a value of undefined and also return true if the variable has a null value.

let userName:string|undefined;

if(userName==null){
    alert(' User Name is undefined ');    
}
else{
    alert(` User Name is null `);
}

If using “===” then an error will occur because this will also check the value type:

Check for Root Level Undefined

If the variable is undefined and you use “==” at the root level to check for undefined, you will get a ReferenceError exception. So to define a variable with a value undefined at the root level, you need to additionally use typeof.

let userNumber:Number|undefined;

if (typeof userNumber == 'undefined') {
    alert(`User Number is ${userNumber}`);
}

About this solution. Learn more.

Check Undefined and Null at the same time in Typescript

Because “==” only checks the variable’s value, if you check for undefined using null in the if condition, the if block will do the same for null. The below code will suggest how to perform the desired operation for the desired type:

var LearnShareIT1: any;
var LearnShareIT2: any = null;

function Check(x:any, ID:any){
    if (x == null) {
        console.log(ID + ' == null');
    }
    if (x === null){
        console.log(ID + ' === null');
    }
    if (typeof x === 'undefined'){
        console.log(ID + ' is undefined');
    }
}

Check(LearnShareIT1, 'LearnShareIT1');
Check(LearnShareIT2, 'LearnShareIT2');

The first condition executes for both value types undefined and null, The following two conditions check the type and type value match to perform the requested action.

Summary

This article has shown you how to check for undefined in TypeScript in specific cases. We hope the information in this article is helpful to you.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *