Skip to content
Related Articles

Related Articles

Improve Article

JavaScript undefined Property

  • Last Updated : 06 Jul, 2020

Below is the example of the undefined Property.

  • Example:




    <script>
        var a ="Geeksforgeeks"
        function test() {
            if (typeof a === "undefined") {
                txt = "'a' is undefined";
            } else {
                txt = "'a' is defined";
            }
            document.write(txt);
        }
        test();
    </script>
  • Output:
    'a' is defined

The undefined property is used to check if a value is assigned to a variable or not.

Syntax:

var x;
if (typeof x === "undefined") {
  txt = "x is undefined";
} else {
  txt = "x is defined";
}

Return Value: It returns ‘defined’ if the variable is assigned any value and ‘undefined’ if the variable is not assigned any value.

More example code for the above property are as follows:
Program 1:




<!DOCTYPE html>
<html>
  
<body>
    <center>
        <h1 style="color: green">
        GeeksforGeeks
    </h1>
        <button onclick="test()">
        Press
    </button>
        <h4>
        Click on the Press button to 
        check if "a" is defined or undefined.
    </h4>
        <p id="gfg"></p>
        <script>
            function test() {
                if (typeof a === "undefined") {
                    txt = "'a' is undefined";
                } else {
                    txt = "'a' is defined";
                }
                document.getElementById(
                "gfg").innerHTML = txt;
            }
        </script>
    </center>
</body>
  
</html>

Output:

Supported Browsers:

  • Google Chrome
  • Firefox
  • Internet Explorer
  • Opera
  • Safari



My Personal Notes arrow_drop_up
Recommended Articles
Page :