Undefined and null types

Undefined type

The undefined type refers to those variables or object properties that are either undefined or do not exist. When a variable, for example, is declared without assigning a value to it, it is of undefined type. So

var iAge;

declares a variable called iAge. Its type is undefined; it can be confirmed by simply outputting its type using the typeof operator:

alert ("iAge is of type: " + typeof(iAge));

So our output will show that iAge is of type undefined:

Output showing iAge of type undefined

So as long as no value is assigned to a variable, its type is undefined. Once a value is assigned, it will no longer be of type undefined but rather one of other types supported in JavaScript: numbers, strings, Boolean, or Null.

Null type

The null type indicates an empty value. When a variable is assigned the value null, its type becomes null. A null type variable is a placeholder that represents nothing. The following shows an example of a variable of type null:

var iAge = null;

Since null is an empty object, its type is object. We can confirm this by using the typeof operator:

alert ("iAge is of type: " + typeof(iAge));

That will output:

Output showing iAge of type null (an empty object)