The undefined
property indicates that a variable has not been assigned a value.
undefined |
Yes | Yes | Yes | Yes | Yes |
The following code shows how to Test if variables are undefined.
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<!--from w w w .j av a 2 s . c o m-->
<script>
function myFunction() {
var t1 = "myVar";
var t2;
if (t1 === undefined) {
txt1 = "t1 is undefined";
} else {
txt1 = "t1 is defined";
}
if (t2 === undefined) {
txt2 = "t2 is undefined";
} else {
txt2 = "t2 is defined";
}
txt = txt1 + "<br>" + txt2;
document.getElementById("demo").innerHTML = txt;
}
</script>
</body>
</html>
The code above is rendered as follows: