5.14 Exercise: The Address Object

Here is the first exercise of the section. Create an address object with three properties: street, city and zipCode. Then write a function called showAddress that takes such an address object and prints each property with its value. Pause the video, do the exercise, then come back to compare with the solution.

One possible solution

  • Use the object literal syntax to create the address — let or const both work here.
  • Use a for...in loop to walk over each property name in the object.
  • Use bracket notation address[key] to read the value for a dynamic key.
const address = {
  street: "...",
  city: "...",
  zipCode: "..."
};

function showAddress(address) {
  for (let key in address) {
    console.log(key, address[key]);
  }
}

showAddress(address);

The object literal syntax ({ ... }) gives us a quick way to define and initialise the address. The for...in loop assigns each property name to key at every iteration, and bracket notation lets us look up the matching value because the key is computed at runtime. Save the changes and you will see every property and its value printed on the console.

That's it for this little exercise on the address object — see you in the next video, where we'll rebuild the same idea with factory functions and constructor functions.

Summary

This lesson guides learners through creating an address object in JavaScript with three core properties: street, city, and postal code. Students learn to build a function that dynamically displays all object properties using a for...in loop and bracket notation, reinforcing key concepts of object creation and property enumeration.

Key points

  • Creating a JavaScript object with multiple properties using object literal syntax
  • Using a for...in loop to iterate over and enumerate all properties in an object
  • Accessing object property values dynamically using bracket notation (obj[property])
  • Building a reusable function that accepts an object as a parameter and displays all its key-value pairs
  • Understanding the relationship between property names and their corresponding values

FAQ

What is the for...in loop and when should you use it?

The for...in loop iterates through all enumerable properties of an object. It's particularly useful when you need to access every property of an object without knowing the property names in advance, or when you want to dynamically display or process all properties.

What is the difference between bracket notation and dot notation for accessing object properties?

Dot notation (like address.street) is used when you know the property name at write time, while bracket notation (like address[propertyName]) allows dynamic property access using variables, making it ideal for loops where the property name changes with each iteration.

Why pass an object to a function instead of accessing its properties directly?

Passing objects to functions enables code reuse and flexibility. A single function like showAddress() can work with any address object regardless of its specific values, making your code more modular, maintainable, and scalable.