Undefined Data Types in JavaScript
•JavaScript•2 min read

JavaScriptUndefinedPrimitive DataTypesJavaScript Basics
Explore the undefined data type in JavaScript—what it means, how it occurs, and where it's commonly used.
Primitive Data Types in JavaScript: Undefined
The fifth primitive data type in JavaScript is the undefined type.
1. Undefined Type
- In JavaScript,
undefinedindicates the absence of a defined value. It means that a variable has been declared but has not been assigned any value. - Example: Unassigned Variable
let name;
document.write(name); // Output: undefinedExplanation:
- In this example, the variable
nameis declared but not assigned a value, so it automatically holds the valueundefined.
2. Common Uses of Undefined
undefinedis used in several scenarios in JavaScript:- Unassigned Variables
- When a variable is declared but not initialized with a value, it automatically gets the value
undefined.
- When a variable is declared but not initialized with a value, it automatically gets the value
let name;
document.write(name); // Output: undefined- Missing Object Properties
- If an object does not have a particular property, accessing that property will return
undefined.
- If an object does not have a particular property, accessing that property will return
let person = {
name: "John",
age: 30
};
// Access a non-existing property
document.write(person.address); // Output: undefinedExplanation:
-
In this example, the object
personhas propertiesnameandage. -
Trying to access the non-existent
addressproperty returnsundefined. -
Functions Without a Return Value
- If a function does not explicitly return a value, it will return
undefined.
- If a function does not explicitly return a value, it will return
function greet() {
console.log("Hello");
}
let result = greet();
document.write(result); // Output: undefinedExplanation:
- The function
greetdoes not return anything, so when we store itsresultin the result variable, it holds the valueundefined.
Summary:
- Undefined is used when a variable is declared but not initialized.
- It is also returned when trying to access non-existing object properties or when functions do not explicitly return a value.



