Table of Contents
In JavaScript, a variable without a value has the value undefined. The type is also undefined.
A variable declared without a value will automatically have the value undefined.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript undefined</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Undefined Variables</h4>
<p id="demo"></p>
<script>
let car;
// car is declared but has no value
document.getElementById("demo").innerHTML = car;
</script>
</body>
</html>

Result View Example
You can use typeof to check if a variable is undefined.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript undefined</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>The Type of undefined</h4>
<p id="demo"></p>
<script>
let car;
// typeof returns "undefined"
document.getElementById("demo").innerHTML = typeof car;
</script>
</body>
</html>

Result View Example
An empty string has a value and a type. It is not the same as undefined.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript undefined</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Empty Values</h4>
<p id="demo"></p>
<script>
let text = "";
// Empty string has a value and a type
document.getElementById("demo").innerHTML =
text + "<br>" + typeof text;
</script>
</body>
</html>

Result View Example
Accessing a non-existing object property returns undefined.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript undefined</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Objects and undefined</h4>
<p id="demo"></p>
<script>
const person = {firstName: "John", lastName: "Doe"};
// person.age does not exist → undefined
document.getElementById("demo").innerHTML = person.age;
</script>
</body>
</html>

Result View Example
A function without a return value returns undefined.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript undefined</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Functions and undefined</h4>
<p id="demo"></p>
<script>
function myFunction() {
let x = 5;
}
// No return statement → undefined
document.getElementById("demo").innerHTML = myFunction();
</script>
</body>
</html>

Result View Example
Objects can be emptied by setting the value to 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.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript undefined</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>You Can Empty an Object</h4>
<p id="demo"></p>
<script>
let person = {firstName: "John", lastName: "Doe"};
let text = "Before: " + person.firstName + " " + person.lastName + "<br>";
// Empty the object by setting it to undefined
person = undefined;
text += "After (undefined): " + person;
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>

Result View Example
Document in project
You can Download PDF file.