Null in TypeScript

The null in TypeScript is a special value & also a data type. The value null represents the intentional absence of any object value. It is one of TypeScript’s primitive values and is treated as falsy for boolean operations. The value of null is represented in using the literal null.

What is Null Value in TypeScript

The value null represents the intentional absence of any object value. It represents nothing or no value. null means we know that the variable does not have any value. TypeScript does not set the value of a variable to null. We need to do set it explicitly. To do that we assign the literal null to the variable

TypeScript also has an undefined value and it means the value is not assigned or we do not know its value.

Take an example of the following person function which returns the person object. It has two fields. name & dateOfMarriage. The name is a required field, but the dateOfMarriage is optional because not everyone is married.

Now, what value do you store in dateOfMarriage if the person is not married?. We explicitly set its value to null to indicate there is no value, which implies that the person is not married.

Null Data Type

Null is also a Data Type in TypeScript.

We can create a null variable by annotating the variable with null. Declaring a variable as of type null does not give it a value of null, but it gets the default value of undefined. We must assign the value null to it.

The only values that you can assign to a null variable are null. You can also assign undefined if you are using <= TypeScript 1.8 or you have disabled strictNullCheck.

Non Nullable Types

TypeScript 2.0 Introduced the strictNullChecks. Prior to that, you can assign null to variables of other Data Types.

Since TypeScript 2.0 you can assign null only to a variable of type null or any

To Make variables of other data types nullable, we need to explicitly specify it. We can make use of the union type, which allows us to construct a type that is a union of two or more types. In the following example, the numVar is a union type containing the numbernull & undefined types.

You can revert to the old behavior if you disable strictNullChecks

Typeof null is object

Although the data type of null value is null, the TypeOf operator incorrectly returns “object”. This is a very old bug in JavaScript. The bug is not fixed as it will break many existing codes.

Checking for Null

You cannot use typeof operator to check for null as it returns “object”. Hence you can either use the == or ===

The undefined is also treated as having no value. Hence running a loose equality checker between them will return true

That is the reason why you should use the strict equality checker which also checks the data type.

null is falsy

The null along with false0''undefinedNaN is considered as falsy values in JavaScript. Whenever JavaScript encounters a null in an expression it implicitly coerces it to false

But that does not mean that null is false. It is neither false nor true. null does not have any value

Leave a Comment

Your email address will not be published.

Scroll to Top