Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check undefined in Typescript

Tags:

typescript

I am using this code to check undefined variable but it's not working.

var  uemail = localStorage.getItem("useremail");

if (typeof uemail === "undefined")
{
    alert('undefined');
}
else
{
    alert('defined');
}
like image 676
pushp Avatar asked May 01 '17 08:05

pushp


People also ask

How do you check if an object is undefined in TypeScript?

To check for undefined in TypeScript, use a comparison to check if the value is equal or is not equal to undefined , e.g. if (myValue === undefined) {} or if (myValue !== undefined) {} .

How could you check null and undefined in TypeScript?

To make a variable null we must assign null value to it as by default in typescript unassigned values are termed undefined. We can use typeof or '==' or '===' to check if a variable is null or undefined in typescript.

How do you check if it's undefined?

In a JavaScript program, the correct way to check if an object property is undefined is to use the typeof operator. If the value is not defined, typeof returns the 'undefined' string.


1 Answers

In Typescript 2 you can use Undefined type to check for undefined values. So if you declare a variable as:

let uemail : string | undefined;

Then you can check if the variable z is undefined as:

if(uemail === undefined)
{

}
like image 61
ashish Avatar answered Oct 08 '22 21:10

ashish