Difference between Null and Undefined

Last Updated : 5 Apr 2026

In TypeScript, null and undefined are two important types that are used to represent no value or the absence of a value. We may face a scenario during development where we need to declare a value and initialize it later. It happens when we do API calls where we have to initialize the variables after getting the API response. In that situation, we can make use of these two special types: null and undefined.

Difference between Null and Undefined

What is Null?

In TypeScript, 'null' is a primitive value that indicates that no value is assigned to the variable. We assigned it to the variable explicitly to indicate that the variable is empty or nothing is contained by it.

We use it usually in the given situations:

  • Indicate "No Value": It is utilized when we intentionally want to represent that a variable or object property holds no value.
  • Erase and Reset Variables: We utilize null to reset a variable or erase its reference to an object.

Example:

Let us look at the basic utilization of null.

Code:

Output:

null
object

Explanation:

In the code above, a variable named value is defined and we explicitly assigned it null. Here, the console.log(value) prints null and the console.log(typeof value) prints "object" and because of a historical JavaScript bug, typeof null returns "object", even though null is a primitive value.

What is Undefined?

When we declare a variable and have not assigned it any value then TypeScript spontaneously allocated the undefined value to the variable of type 'undefined'. We can also directly assign an undefined value to the variable whose type is 'undefined'.

Undefined is a primitive value. In certain situations, it is automatically allocates to variables:

  • Uninitialized Variables in TypeScript: When a variable is declared but we have not initialized it then it will have the value undefined.
  • Missing Function Return Value: When we miss return value in function then it returns undefined by default.
  • Non-Existent Array Elements or Object Properties: When we try to get an object property or an array element that is not present in the array or object then it results in undefined.

Example:

Output:

undefined
"undefined" 

Explanation:

In this example, we have defined a variable named a utilizing the var keyword and have not assigned any value to it and logged the output in the console which returns undefined. When we verify the type of the variable, it shows "undefined".

Null vs. Undefined

The important differences between null and undefined are:

SNnullundefined
1.It is an assignment value. It can be explicitly assigned to a variable to indicate that the variable does not point to any object.It is not automatically assigned. It indicates that a variable exists but we have not assigned it a value. It must be assigned manually.
2.It is a primitive value. When we print typeof null, it returns "object" because of a historical JavaScript bug.It is a primitive value and its own type. The typeof undefined returns "undefined".
3.It represents the intentional absence of any object or value.We use undefined to represent the absence of a value. When a variable is declared but not initialized, a function returns nothing, or a property/element does not exist.
4.It indicates that a variable has no value assigned intentionally.It shows that a variable exists, but we have not assigned it a value.
5.It converts to 0 in numeric operations.It converts to NaN in numeric operations.

Conclusion

In this article, we have covered the difference between the null and undefined keywords. We use them to represent the absence of a value but in distinct ways. The term undefined represents that we have defined a variable but no value is assigned to it. While null is assigned intentionally to represent a variable that holds no value. The understanding of the difference between null and undefined helps developers to write clearer code, prevent unintended errors and manage data flow in applications.