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.

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:
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.
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:
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".
The important differences between null and undefined are:
| SN | null | undefined |
|---|---|---|
| 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. |
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.
We request you to subscribe our newsletter for upcoming updates.