JavaScript accessing objects

 JavaScript accessing objects

You already know what object's in JavaScript are. We also briefly discussed how to access them in the same lesson but let's elaborate them in this lesson.

There are two ways to access objects in JavaScript:

  • Dot notation which is represented by .
  • Bracket notation which is represented by []
Let's take an example to understand these further. In the first instance when displaying the value of property2 we are using the dot notation, and while accessing the property 4 we are suing the bracket notation. It's useful when we have spaces in key's.

<!DOCTYPE html>
<html>
<body>
<p id="result"></p>
    <script>
        var object1 = {
            property1 : "value1",
            property2 : "value2",
            property3 : "value3",
            "property 4" : "value4"
     };
     document.getElementById("result").innerHTML = 
     object1.property2 + "<br>" + object1["property 4"];
     </script>

</body>
</html>



Post a Comment

0 Comments

React