Objects
A tour is not just a name. It has a name, a price, a difficulty level, and an availability flag. These values belong together. An object lets you group related values under a single variable with named properties.
What an object is
Section titled “What an object is”An object is a collection of key-value pairs, where each key is a string (the property name) and each value can be any type:
const tour = { name: 'Cascade Ridge Hike', price: 149, difficulty: 'moderate', available: true,};This is an object literal: curly braces containing comma-separated key: value pairs. The trailing comma after the last property is optional but conventional — it makes adding new properties easier.
Dot notation
Section titled “Dot notation”Access a property’s value with a dot followed by the property name:
console.log(tour.name); // 'Cascade Ridge Hike'console.log(tour.price); // 149console.log(tour.difficulty); // 'moderate'console.log(tour.available); // trueBracket notation
Section titled “Bracket notation”Bracket notation accesses a property using a string in square brackets:
console.log(tour['name']); // 'Cascade Ridge Hike'console.log(tour['price']); // 149Bracket notation is useful when the property name is stored in a variable:
const field = 'price';console.log(tour[field]); // 149Use dot notation by default. Switch to bracket notation when the property name is dynamic.
Adding properties
Section titled “Adding properties”Assign to a property that does not exist yet and JavaScript creates it:
tour.maxGroupSize = 8;console.log(tour.maxGroupSize); // 8Updating properties
Section titled “Updating properties”Assign to an existing property to change its value:
tour.price = 169;console.log(tour.price); // 169Like arrays declared with const, a const object’s properties can still be modified. const only prevents reassigning the variable itself.
Accessing a missing property
Section titled “Accessing a missing property”If you access a property that does not exist, JavaScript returns undefined — it does not throw an error:
console.log(tour.rating); // undefinedThis is why TypeError: Cannot read properties of undefined is a common bug: you access a property of an object, get undefined, then try to access a property of that undefined result.
console.log(tour.rating.average); // TypeError — tour.rating is undefinedArrays of objects
Section titled “Arrays of objects”Objects and arrays work together to represent collections of structured records. This is the pattern you will use for tour listings throughout the rest of the course:
const tours = [ { name: 'Cascade Ridge Hike', price: 149, difficulty: 'moderate', available: true }, { name: 'Summit Loop Trek', price: 199, difficulty: 'hard', available: true }, { name: 'Valley Floor Walk', price: 99, difficulty: 'easy', available: false },];
console.log(tours[0].name); // 'Cascade Ridge Hike'console.log(tours[1].price); // 199console.log(tours[2].available); // falseRead this as: “an array of tour objects.” Each element is an object; each object has the same shape (same properties). This consistent shape is what makes them easy to loop over — which you will do in Module 03.
Exercise
Section titled “Exercise”const tour = { name: 'Cascade Ridge Hike', price: 149, difficulty: 'moderate', available: true,};
// Access each property with dot notationconsole.log(tour.name);console.log(tour.price);console.log(tour.difficulty);console.log(tour.available);
// Update the pricetour.price = 169;console.log(tour.price); // 169
// Add a new propertytour.maxGroupSize = 8;console.log(tour.maxGroupSize); // 8
// Log the final objectconsole.log(tour);Then create a second tour object for 'Summit Loop Trek' with price 199, difficulty 'hard', available true, and put both in an array. Access the second tour’s name via the array.
- An object groups related values under named properties using
{ key: value }syntax. - Dot notation (
object.property) is the standard way to read and write properties. - Bracket notation (
object['property']) is used when the property name is a variable. - Assigning to a new property name creates it; assigning to an existing one updates it.
- Accessing a missing property returns
undefined— it does not throw an error. - Arrays of objects — a collection of same-shaped records — are the core data structure for tour listings and similar collections.