Undefined in JavaScript

Undefined is a primitive data type in JavaScript. It is also a special value in JavaScript. JavaScript also has a global variable with the name undefined which has the value Undefined. It is one of JavaScript’s primitive values and is treated as falsy for boolean operations.

Undefined in JavaScript

When we refer to undefined in JavaScript, it could be one of the following

  1. Undefined as a value
  2. Undefined Data Type
  3. Global Undefined variable

Undefined as a 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, JavaScript assigns the undefined value to it. It is an unintentional absence of any value. Undefined also means non-existing property of an object or non-existing array element etc.

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”.

JavaScript explicitly sets the value undefined when we do not provide any value. The following are some of the instances where a variable gets the value undefined

Uninitialized variable

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

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

Function argument that has not been supplied

In JavaScript number of arguments that we supply does not have to match the Parameters of the functions. The unpassed arguments are set to undefined.

In the following example, we do not pass any argument to the parameter c. Here JavaScript assigns undefined to c argument.

The return value of functions that don’t return a value

When a function has no return value, it returns undefined.

In the following example, the function someFunc does not have a return value. But when we assign its return value to a variable, JavaScript assigns undefined to it.

Non-existing object Property

Trying to access a non-existing property of an object returns undefined.

In the example below, the person object does not have age property. Trying to access it does not throw any errors but returns undefined instead.

Non-existing array elements

Similarly, Non-existing array elements also return undefined.

In the following example, the cars array does not have an element at 5. But JavaScript returns undefined.

Note that referring non-existent variable returns reference error. But the referring non-existent property of an object (or non-existent array element) 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 explicitly set a 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 in JavaScript. The data type of variable with the value of undefined is undefined. The variable of undefined type can store only one value i.e. undefined.

In the following example, we use the typeof operator to find out the data type of num variable.

Global Undefined variable

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

It 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.

We can explicitly assign undefined to a variable. The undefined right-hand side of the assignment operator refers to the global variable.

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 the 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.

The typeof is the preferred way of checking for undefined because the global undefined property can be overwritten

Null & undefined

Comparing undefined to null using loose equality checker (==) return true. This is because JavaScript loose the 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.

The boolean value of undefined

The boolean value of undefined is considered as falsy. i.e. JavaScript 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 results in false.

Arithmetic Expression & undefined

In arithmetic expressions, the undefined is coerced to NaN.


  1. ECMA Specifications
  2. Undefined definition – MDN

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top