Accessing Properties with Dot Notation
You'll learn how to read and retrieve values stored inside objects using the simple, readable dot operator.
The Core Idea
Now that you know how to create object literals with properties, you need a way to get those values back out. The dot notation is the most straightforward syntax: you write the object's name, followed by a dot (.), followed by the property name you want to access.
Think of an object like a filing cabinet, and each property is a labeled drawer. The dot notation is like pointing to the cabinet and then pointing to the specific drawer label: "Hey cabinet, give me what's in the 'name' drawer!"
Reading Properties
const person = {
name: "Alice",
age: 30
};
console.log(person.name); // "Alice"
console.log(person.age); // 30
The dot connects the object to its property. person.name means "access the name property of the person object."
Writing Properties
You can also use dot notation to change existing properties or add new ones:
const car = {
color: "red"
};
car.color = "blue"; // Update existing property
car.model = "Sedan"; // Add new property
console.log(car.color); // "blue"
console.log(car.model); // "Sedan"
Rules and Limitations
The dot notation only works with property names that are valid identifiers (no spaces, don't start with numbers, etc.). If your property name has spaces or special characters, you'll need a different approach (coming in future lessons).
Key Takeaway: Use dot notation (object.property) to read or write object properties—it's the cleanest and most common way to access object data when property names are valid identifiers.