Undefined
The undefined
keyword represents an uninitialized or missing value. Any variable declared but not explicitly assigned a value will have undefined by default.
Behavior
Accessing an uninitialized variable returns undefined.
let x;
console.log(x); // undefined
Functions that do not explicitly return a value return undefined.
fn doNothing() {
const value = 1 + 1;
}
console.log(doNothing()); // undefined
Checking for undefined
Use the comparison operator (== or ===) to check for undefined
.
if(x === undefined)
console.log("x is undefined!");
⚠️ Don't use
!x
as you will get a runtime error ifx
is not convertible to a boolean! (See Boolean convertible types)