Skip to content

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.

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.

Access a property’s value with a dot followed by the property name:

console.log(tour.name); // 'Cascade Ridge Hike'
console.log(tour.price); // 149
console.log(tour.difficulty); // 'moderate'
console.log(tour.available); // true

Bracket notation accesses a property using a string in square brackets:

console.log(tour['name']); // 'Cascade Ridge Hike'
console.log(tour['price']); // 149

Bracket notation is useful when the property name is stored in a variable:

const field = 'price';
console.log(tour[field]); // 149

Use dot notation by default. Switch to bracket notation when the property name is dynamic.

Assign to a property that does not exist yet and JavaScript creates it:

tour.maxGroupSize = 8;
console.log(tour.maxGroupSize); // 8

Assign to an existing property to change its value:

tour.price = 169;
console.log(tour.price); // 169

Like arrays declared with const, a const object’s properties can still be modified. const only prevents reassigning the variable itself.

If you access a property that does not exist, JavaScript returns undefined — it does not throw an error:

console.log(tour.rating); // undefined

This 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 undefined

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); // 199
console.log(tours[2].available); // false

Read 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.

const tour = {
name: 'Cascade Ridge Hike',
price: 149,
difficulty: 'moderate',
available: true,
};
// Access each property with dot notation
console.log(tour.name);
console.log(tour.price);
console.log(tour.difficulty);
console.log(tour.available);
// Update the price
tour.price = 169;
console.log(tour.price); // 169
// Add a new property
tour.maxGroupSize = 8;
console.log(tour.maxGroupSize); // 8
// Log the final object
console.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.