Undefined Vs Null in JavaScript (original) (raw)
Last Updated : 17 Sep, 2024
In JavaScript, both undefined and null represent the absence of a meaningful value, but they have different purposes and are used in distinct contexts. Knowing when and how to use each can help you write clearer, more predictable code. Let's see the differences between undefined and null in JavaScript.
What is undefined?
undefined is a primitive value automatically assigned to variables in certain situations:
- Uninitialized Variables: A variable that is declared but not initialized will have the value undefined.
- Missing Function Return: If a function does not explicitly return a value, it returns undefined by default.
- Non-Existent Object Properties or Array Elements: Accessing an object property or an array element that does not exist results in undefined.
Example: In the example, we have shown undefined.
JavaScript `
let x; // variable declared but not initialized console.log(x); // Output: undefined
function doSomething() { // no return statement, so the function returns undefined } console.log(doSomething()); // Output: undefined
let obj = {}; console.log(obj.property); // Output: undefined
`
Output
undefined undefined undefined
What is Null?
null is a special value in JavaScript that represents the deliberate absence of any object value. It is often used to:
- Indicate "No Value": null explicitly shows that a variable or object property should have no value.
- Reset or Clear Variables: Use null to reset a variable or remove its reference to an object.
Example: In the example, we have shown null .
JavaScript `
let y = null; // variable intentionally set to null console.log(y); // Output: null
let obj = { property: null }; // property intentionally set to null console.log(obj.property); // Output: null
`
You can see refer to “==” vs “===” article.
null == undefined // true
null === undefined // false
It means null is equal to undefined but not identical.
When we define a variable to undefined then we are trying to convey that the variable does not exist . When we define a variable to null then we are trying to convey that the variable is empty.
Difference Between undefined and null
undefined | Null |
---|---|
Indicates a variable that has been declared but not yet assigned a value. | Represents the intentional absence of any object value. |
Primitive data type in JavaScript. | Primitive data type in JavaScript. |
Automatically assigned to variables that are declared but not initialized. | Must be explicitly assigned to a variable. |
undefined == null // true (loose equality considers them equal). | undefined == null // true (loose equality considers them equal). |
undefined === null // false (they are not strictly equal in type). | null === undefined // false (they are not strictly equal in type). |
Used by JavaScript when a variable is not assigned a value. | Used intentionally by developers to indicate "no value" or "empty". |
undefined is a global property with the value undefined . | null is a global property with the value null . |
undefined values are omitted during serialization. | null values are preserved during JSON serialization (e.g., {"key": null} ). |
Conclusion
undefined indicates a variable hasn’t been initialized, while null is intentionally assigned to indicate no value. Understanding the distinction helps write cleaner, more predictable code in JavaScript, especially when handling default values or checking for missing data.