63. Accessing Object Properties
There are two main ways to retrieve data stored in an object.
1. Dot Notation (Preferred)
This is the simplest way to access a property, assuming the property name is a valid, continuous identifier.
javascript const car = { make: 'Honda', model: 'Civic' };
console.log(car.make); // Output: Honda
2. Bracket Notation
Bracket notation ([]) is required when:
- The property name contains spaces or special characters (must be quoted).
- You are using a variable to access the property key (Dynamic Access).
javascript const person = { 'full name': 'Alex Smith', city: 'Seattle' };
// Case 1: Key with space console.log(person['full name']); // Output: Alex Smith
// Case 2: Dynamic access using a variable let keyName = 'city'; console.log(person[keyName]); // Output: Seattle
Note: You cannot use dot notation with a variable (person.keyName would look for a property named 'keyName', not 'city').