A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/undefined-vs-null-in-javascript/ below:

Undefined Vs Null in JavaScript

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:

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:

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.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4