JavaScript undefined Primitive
A undefined primitive represents the absence of an assigned value.
The type name is undefined.
Undefined values are one of JavaScript's 7 primitive types.
The Undefined Value
JavaScript has exactly one undefined value:
undefined
The primitive type name is undefined.
Type Information
Uninitialized Variables
Variables declared without a value are undefined.
Example
let x;
document.getElementById("demo").innerHTML = x;
The result is:
undefined
Missing Object Properties
Reading a non-existing property returns undefined.
Example
const person = {name:"John"};
document.getElementById("demo").innerHTML = person.age;
The result is:
undefined
Missing Function Arguments
Parameters that are not supplied receive the value undefined.
Example
function myFunction(x) {
return x;
}
document.getElementById("demo").innerHTML = myFunction();
The result is:
undefined
Functions Without Return Values
Functions that do not return a value return undefined.
Example
function myFunction() {
let x = 1;
}
document.getElementById("demo").innerHTML = myFunction();
The result is:
undefined
Comparing with Undefined
Example
let x;
document.getElementById("demo").innerHTML = (x === undefined);
The result is:
true
Undefined vs Null
undefined and null are different values.
| Expression | Result |
|---|---|
undefined == null |
true |
undefined === null |
false |
typeof undefined |
"undefined" |
typeof null |
"object" |
Falsy Value
Undefined is a falsy value.
Example
let x;
if (x) {
document.getElementById("demo").innerHTML = "TRUE";
} else {
document.getElementById("demo").innerHTML = "FALSE";
}
The result is:
FALSE
Summary
| Property | Value |
|---|---|
| Primitive Type | undefined |
| Possible Values | One (undefined) |
| Falsy | Yes |
| Object | No |