Skip to content

Instantly share code, notes, and snippets.

@gladchinda
Created April 18, 2018 09:35
Show Gist options
  • Save gladchinda/2af6523cced576a7a59133cfc964da34 to your computer and use it in GitHub Desktop.
Save gladchinda/2af6523cced576a7a59133cfc964da34 to your computer and use it in GitHub Desktop.
const student = {
name: 'John Doe',
age: 16,
scores: {
maths: 74,
english: 63,
science: 85
}
};
// Without Destructuring
function displaySummary(student) {
console.log('Hello, ' + student.name);
console.log('Your Maths score is ' + (student.scores.maths || 0));
console.log('Your English score is ' + (student.scores.english || 0));
console.log('Your Science score is ' + (student.scores.science || 0));
}
// With Destructuring
function displaySummary({name, scores: { maths = 0, english = 0, science = 0 }}) {
console.log('Hello, ' + name);
console.log('Your Maths score is ' + maths);
console.log('Your English score is ' + english);
console.log('Your Science score is ' + science);
}
displaySummary(student);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment