JavaScript Data Type: Undefined
1 Introduction
The Undefined data type in JavaScript is a primitive type that represents a variable that
has been declared but not assigned a value, or a function that returns nothing explicitly.
2 Characteristics
• Default Value: Automatically assigned to uninitialized variables.
• Type Check: typeof undefined returns "undefined".
• Distinct from Null: Indicates absence of assignment, not an intentional empty
value.
3 Use Cases
Undefined is used for:
• Checking if variables are initialized before use.
• Handling cases where functions do not return a value.
• Managing default parameters in functions.
4 Example
Below is a JavaScript code example demonstrating the Undefined data type:
1 let x ; // Declared but not assigned
2 console . log ( x ) ; // Output : undefined
3 console . log ( typeof x ) ; // Output : undefined
4 function doNothing () {} // No return statement
5 let result = doNothing () ;
6 console . log ( result ) ; // Output : undefined
7 if ( typeof x === " undefined ") {
8 console . log (" x is not initialized !") ; // Output : x is not
initialized !
9 }
1
5 Notes
• Avoid explicitly setting variables to undefined; use null for intentional empty
values.
• Commonly used in error checking or conditional logic.
• Be cautious when accessing object properties that may be undefined.