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
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Difference Between IPv4 and IPv6 In the digital world, where billions of devices connect and communicate, Internet Protocol (IP) Addresses play a crucial role. These addresses are what allow devices to identify and locate each other on a network.To know all about IP Addresses - refer to What is an IP Address?Currently, there are tw
9 min read
Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w
8 min read
Difference between BFS and DFS Breadth-First Search (BFS) and Depth-First Search (DFS) are two fundamental algorithms used for traversing or searching graphs and trees. This article covers the basic difference between Breadth-First Search and Depth-First Search.Difference between BFS and DFSParametersBFSDFSStands forBFS stands fo
2 min read
Differences between TCP and UDP Transmission Control Protocol (TCP) and User Datagram Protocol (UDP) both are protocols of the Transport Layer Protocols. TCP is a connection-oriented protocol whereas UDP is a part of the Internet Protocol suite, referred to as the UDP/IP suite. Unlike TCP, it is an unreliable and connectionless pr
9 min read
NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net
15+ min read