Thursday, 30 August 2018

JavaScript: in: Check for property existence in object

'in' operator is used to check for the existence of a property in an object.

Syntax
property in object

Above statement return true if object has given property, else false.

in.html

<!DOCTYPE html>

<html>

<head>
    <title>in operator</title>
</head>

<body>
    <script type="text/javascript">
        var person1 = {
            firstName: "Hari Krishna",
            lastName: "Gurram",
            address: {
                city: "Bangalore",
                area: "Marthalli"
            }
        };

        // Print "person1 has property firstName"
        if ("firstName" in person1) {
            document.write("person1 has property firstName <br />");
        } else {
            document.write("person1 don't have property firstName <br />");
        }

        // Print "person1 don't have property country"
        if ("country" in person1) {
            document.write("person1 has property country <br />");
        } else {
            document.write("person1 don't have property country <br />");
        }
    </script>
</body>

</html>



Previous                                                 Next                                                 Home

No comments:

Post a Comment