Mastery
JavaScript » Objects

Accessing Object Properties

You can access object properties in two ways:

name = person.lastName;
name = person["lastName"];

Displays “Sherlock Holmes”:

<p id="demo"></p>

<script>
var person = {
    firstName: "Sherlock",
    lastName : "Holmes",
    id       :  221B
};

document.getElementById("demo").innerHTML = person.firstName + " " + person["lastName"];
</script>

Accessing Object Methods

You can call an object method with the following syntax:

name = objectName.methodName();