The undefined value is a condition where an expression does not have a correct value. JavaScript undefined property indicates that a variable has not been assigned a value, or not declared at all.
Example JavaScript undefined
A simple example code will get an undefined value when you call a non-existent property or method of an object.
Variable not declared:
<!DOCTYPE html>
<html>
<body>
<script>
if (typeof myVar === "undefined") {
console.log("myVar is undefined")
} else {
console.log("myVar is defined")
}
</script>
</body>
</html>
Output:

How can I check for “undefined” in JavaScript?
Answer: One reason to use typeof
is that it does not throw an error if the variable has not been declared.
if (typeof myVar !== "undefined") {
doSomething();
}
Or use
myVar === undefined
Warning: Please note that ===
is used over ==
and that myVar
has been previously declared (not defined).
Do comment if you have any doubts or suggestions on this JS undefined variable topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version

Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.