Test if a variable is undefined, whether the variables have been assigned a value or not - Javascript Data Type

Javascript examples for Data Type:undefined

Description

The undefined property indicates that a variable has not been assigned a value.

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {//from   w w  w .j a  v  a 2  s.  co m
    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>

Related Tutorials