Undefined in TypeScript

Undefined is a special value and also a data type in TypeScriptTypeScript also has a global variable with the name undefined which has the value Undefined. Undefined is a primitive value and is treated as falsy for boolean operations. This tutorial let us learn how to use undefined in TypeScript.

Undefined as Value

Undefined is a primitive value that indicates that the value is not assigned. i.e. whenever we do not explicitly assign a value to a variable, TypeScript assigns the undefined value to it. It is an unintentional absence of any value.

Undefined is different from the value null. The null value means we know that it does not have any value. The value undefined means we do not know its value.

The ECMA specifications define undefined as “the primitive value used when a variable has not been assigned a value”.

TypeScript explicitly sets the value undefined when we do not provide any value.

The following are some of the cases where variable gets the value undefined

Uninitialized variable

Every TypeScript variable that we create without assigning any value gets the value of undefined.

The following example declares the variable num of type number. We have not given it any initial value. By default, it gets the value undefined.

Optional Function argument

Typescript does not allow us to omit a function argument unless we explicitly declare it by adding ? in the parameter declaration. When we do not pass a value to an optional parameter, the TypeScript sets its value as undefined.

In the following example we have declared parameter x as optional by adding ? after x. When we invoke the function without passing any value to x Typescript initializes it with undefined.

If we set the type of non-optional parameter as undefined and pass nothing, the TypeScript compiler will throw an error.

Non-existing array elements

Non-existing array elements return undefined. In the following example, the cars array does not have an element at 5. Hence it returns undefined.

Void operator

The void operator always returns undefined irrespective of the expression provided to it.

For Example, all of the following returns are undefined.

Explicitly set to undefined

We can also explicitly set the variable to undefined.

Assigning undefined is not recommended unless you have a good reason to do so. undefined means that the value is not assigned. If you know the variable does not have any value then use null.

Undefined Type

The undefined is also a type. Hence we can create a variable of type undefined

Until Typescript 1.8 the undefined was considered a valid value and assignable to all other data types. But since the introduction of strictNullChecks in TypeScript 2.0, undefined is assignable only to any & void if strictNullChecks is enabled. If disabled, then you can assign undefined to all other data types

Nothing is assignable to undefined except never & any. You can assign null to undefined only if you disable strictNullChecks

You can disable or enable strictNullChecks in tsconfig.json.

Global Undefined variable

The TypeScript has a global variable with the name undefined. The initial value of the global undefined is the value undefined.

Undefined is the property of the global object. Hence you can access it using the window object (only in the browser) or using the globalThis property.

Since the ES5 version, the undefined is a non-configurable, non-writable & non-enumerable property. But that does not stop someone from overriding it inside a function.

Hence take care not to override it.

Checking for undefined

There are two ways you can check if the value is undefined.

One method is to compare the value against the value of global undefined property

Another way is to use the typeof operator, which returns the data type as a string.

Note that undefined is true does not mean that the property or variable exists. To check whether the property exists use the hasOwnProperty or use the 'prop' in obj syntax.

Null & Undefined

Comparing undefined to null using loose equality checker (==) return true. This is because loose equality checker (==) coerces the value of undefined to no value. Hence the result is true.

But using the strict equality checker (===) returns false because the data type of null is different from the data type of undefined.

Boolean value of undefined

The boolean value of undefined is considered as falsy. i.e. TypeScript implicitly converts the value undefined to false before using it in an expression involving Booleans

But this does not mean that undefined is false. Undefined means we do not know its value. Hence comparing undefined either with false or true always result in false

Arithmetic Expression & undefined

In arithmetic expressions, the undefined is coerced to NaN.

Scroll to Top