JavaScript undefined
In JavaScript, a variable without a value has the value undefined.
The type is also undefined.
Undefined Variables
A variable declared without a value will automatically have the value undefined.
The Type of undefined
You can use typeof to check if a variable is undefined.
Empty Values
An empty string has a value and a type.
It is not the same as undefined.
Example
let text = "";
document.getElementById("demo").innerHTML =
text + "<br>" + typeof text;
Try it Yourself »
Objects and undefined
Accessing a non-existing object property returns undefined.
Example
const person = {firstName:"John", lastName:"Doe"};
document.getElementById("demo").innerHTML = person.age;
Try it Yourself »
Functions and undefined
A function without a return value returns undefined.
Example
function myFunction() {
let x = 5;
}
document.getElementById("demo").innerHTML = myFunction();
Try it Yourself »
You Can Empty an Object
Objects can be emptied by setting the value to undefined.
Example
const person = {firstName:"John", lastName:"Doe"};
person = undefined;
Objects can also be emptied by setting the value to null.
Note
undefined is a JavaScript value.
It means that a variable has been declared, but no value has been assigned.