Thursday, 30 August 2018

JavaScript: Nested objects

An object can be nested inside other object.

var person1 = {
         firstName: "Hari Krishna",
         lastName: "Gurram",
         address:{
                  city: "Bangalore",
                  area: "Marthalli"
         }
};

As you see above snippet, address object is embedded inside person1 object.

nested.html
<!DOCTYPE html>

<html>

<head>
    <title>Nested Object</title>
</head>

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

        document.write("firstName : " + person1.firstName + "<br />");
        document.write("lastName : " + person1.lastName + "<br />");
        document.write("city : " + person1.address.city + "<br />");
        document.write("area : " + person1.address.area + "<br />");
    </script>
</body>

</html>



Previous                                                 Next                                                 Home

No comments:

Post a Comment