Undefined Vs Null in JavaScript
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
Outputundefined
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.
Similar Reads
Undefined in JavaScript Undefined is a type of Data type in JavaScript. It is a primitive value undefined, when a variable is declared and not initialized or not assigned with any value. By default, the variable was stored with an Undefined value. Undefined is a global read-only variable that represents the value Undefined
2 min read
What does !== undefined mean in JavaScript ? In JavaScript, !== is a strict inequality operator, and undefined is a special value representing the absence of a value or the lack of an assigned value to a variable. The !== operator checks for both value and type equality, and !== undefined is used to verify if a variable is not equal to the und
1 min read
Null in JavaScript In JavaScript, `null` indicates the deliberate absence of any object value. It's a primitive value that denotes the absence of a value or serves as a placeholder for an object that isn't present. `null` differs from `undefined`, which signifies a variable that has been declared but hasn't been assig
3 min read
What is Undefined X 1 in JavaScript ? JavaScript is one of the most popular lightweight, interpreted compiled programming languages. It is single-threaded and synchronous in nature. Scripts(program in JavaScript) re executed as plain text. They can be either written directly on our page or in an external JavaScript file. JavaScript can
2 min read
JavaScript undefined Property The undefined property is used to check if a value is assigned to a variable or not. Syntax: var x; if (typeof x === "undefined") { txt = "x is undefined"; } else { txt = "x is defined"; } Return Value: It returns 'defined' if the variable is assigned any value and 'undefined' if the variable is not
1 min read
How to Replace a value if null or undefined in JavaScript? In JavaScript, replacing a value if it is null or undefined involves providing a default value to use when the original value is either null or undefined. This ensures the application has valid data, preventing potential errors or unexpected behavior. Here we have some common approaches: Table of Co
2 min read
How to check for "undefined" value in JavaScript ? In JavaScript, undefined is a primitive value that represents the absence of a value or the uninitialized state of a variable. It's typically used to denote the absence of a meaningful value, such as when a variable has been declared but not assigned a value. It can also indicate the absence of a re
2 min read
How to Check empty/undefined/null String in JavaScript? Empty strings contain no characters, while null strings have no value assigned. Checking for an empty, undefined, or null string in JavaScript involves verifying if the string is falsy or has a length of zero. Here are different approaches to check a string is empty or not.1. Using === OperatorUsing
2 min read
What are undeclared and undefined variables in JavaScript? Undefined: It occurs when a variable has been declared but has not been assigned any value. Undefined is not a keyword. Undeclared: It occurs when we try to access any variable that is not initialized or declared earlier using the var or const keyword. If we use 'typeof' operator to get the value of
1 min read
How to handle an undefined key in JavaScript ? In this article, we will try to analyze how we may handle an undefined key (or a property of an object) in JavaScript using certain techniques or approaches (via some coding examples). Firstly let us quickly analyze how we may create an object with certain keys along with their values using the foll
3 min read