Saturday, September 21, 2024
Google search engine
HomeLanguagesJavascriptJavaScript undefined Property

JavaScript undefined Property

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: 

Below are examples of the undefined Property.

Example 1: 

JavaScript




var a ="Geeksforneveropen"
function test() {
    if (typeof a === "undefined") {
        txt = "'a' is undefined";
    } else {
        txt = "'a' is defined";
    }
console.log(txt);
}
test();


Output:

'a' is defined

Example 2: 

HTML




<body>
    <center>
        <h1 style="color: green">
        neveropen
    </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>


Output: 

Supported Browsers:

  • Google Chrome
  • Firefox
  • Internet Explorer
  • Opera
  • Safari
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments