Basics

Challenges

Classes

Cookies

Debugging

Events

External Files

Flow Control

Forms

Functions

Html Elements

Installation

Interfaces

Keywords

Modules

Namespaces

Operators

Reference Files

String

Types

TypeScript - Null Check

This is a great example of null-checking. You can check to see if an object is null by simply using this: if(object){ ... code ...}.

Keep in mind that null is not the same thing as undefined. Nulls are for objects. Undefined is for variables and properties.

 


// test true when name has not been defined.
if(name === undefined)
{
   nameMsg = "Name is undefined.";
}

// tests true when name has no value but has been defined.
if (name === null)
{
   nameMsg = "Name is null.";
}

if (name)
{
   nameMsg = "Name is not null and is " + name + ".";
}