JavaScript > Number Properties

Undefined Property in JavaScript

How to indicate that a variable has not been assigned a value in JavaScript?


By using undefined property we can indicate that a variable has not been assigned a value.

<p>Click the below button to check whether the variable is undefined.</p>
<input type="button" onclick="mySearch()" value="Search">
<p id="myId"></p>

<script>
    function mySearch() {
        var A; //* Here we did not defined with any value to the variable A *//
        if (A === undefined) {
            R = "A is undefined";
        }
        else {
            R = "A is defined";
        }
        document.getElementById("myId").innerHTML = R;
    }
</script>

In the above code snippet we have given Id as "myId"to the second <p> element in the HTML code. There is a function mySearch() in the<script>block which is connected to the onclick of the HTML button and there is a undefined variable A (value of the A variable is not defined). In the next step we declared the if statement, if (A=== undefined) that means if variable A is equal type and equal value, then it returns "A is undefined" other wise the output returns the else statment command ("A is defined"). Onclick of the button "Search" in the HTML code fires the function mySearch() in the <script> block and give the output as 'A is undefined'.

OUTPUT

 Views: 3820 | Post Order: 87



Write for us






Hosting Recommendations