20. The undefined Type
undefined is a primitive value used when a variable has been declared but has not yet been assigned a value by the developer.
Automatic Assignment
javascript let userName; // Declared, but not initialized console.log(userName); // Output: undefined
// If a function doesn't return anything explicitly, it returns undefined: function doNothing() { /* ... */ } console.log(doNothing()); // Output: undefined
Key Difference: null vs. undefined
| Feature | undefined | null |
|---|---|---|
| Meaning | Value not yet assigned (System default) | Intentional absence of value (Developer assigned) |
| Type | undefined | object (historical error) |
Best Practice: Never explicitly set a variable to undefined. Use null if you want to explicitly clear a variable's value.