Course Content
JavaScript Data Structures
JavaScript Data Structures
Challenge: Accessing Object Properties
Task
You're given an object, user
, representing a person's hobbies and interests. Your task is to access and retrieve specific details from the object.
- Access and log the person's name.
- Access the
indoor
hobby and log it to the console. - Store the person's age in a variable.
- Access the favorite music genre and log it.
const user = { name: "Sarah", age: 32, hobbies: { outdoor: "hiking", indoor: "painting", }, preferences: { music: "jazz", movieGenre: "sci-fi", }, }; // Task 1 console.log(___); // person's name // Task 2 console.log(___); // indoor hobby // Task 3 const userAge = ___; console.log(userAge); // person's age // Task 4 console.log(___); // music genre
Expected output:
- Use dot notation to access properties within an object.
- To access properties within nested objects and arrays, chain the dot notation and square brackets (if needed).
- Use assignment with dot notation to store property values in variables.
const user = { name: "Sarah", age: 32, hobbies: { outdoor: "hiking", indoor: "painting", }, preferences: { music: "jazz", movieGenre: "sci-fi", }, }; // Task 1 console.log(user.name); // person's name // Task 2 console.log(user.hobbies.indoor); // indoor hobby // Task 3 const userAge = user.age; console.log(userAge); // person's age // Task 4 console.log(user.preferences.music); // music genre
Thanks for your feedback!