Extract properties into variables directly:
const book = { title: "Dune", author: "Herbert", year: 1965 };
const { title, author } = book;
const { title: bookTitle } = book;
function displayBook({ title, author, year }) {
return `${title} by ${author} (${year})`;
}
displayBook(book); // "Dune by Herbert (1965)"
const scores = { Alice: 95, Bob: 82, Carol: 91 };
for (const [name, score] of Object.entries(scores)) {
console.log(`${name}: ${score}`);
}
Given:
const student = {
firstName: "Jordan",
lastName: "Lee",
scores: [88, 92, 79, 95, 85]
};
firstName and lastName from student.fullName(student) that returns "Jordan Lee".averageScore(student) that returns the average of student.scores (rounded to 1 decimal).#output: "Jordan Lee — avg: 87.8".Object destructuring: const { name, age } = person;