Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Challenge: Retrieve Object Property Values | Fundamentals of JavaScript Objects
JavaScript Data Structures

book
Challenge: Retrieve Object Property Values

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

12345678910111213141516171819202122232425
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
copy

Expected output:

Sarah
painting
32
jazz
  1. Use dot notation to access properties within an object.
  2. To access properties within nested objects and arrays, chain the dot notation and square brackets (if needed).
  3. 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
123456789101112131415161718192021222324
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
copy

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 6
some-alt