Skip to content

Instantly share code, notes, and snippets.

@matheusmurta
Created July 15, 2020 04:32
Show Gist options
  • Save matheusmurta/67fbfd1381f9f912ad502b40fad54b7d to your computer and use it in GitHub Desktop.
Save matheusmurta/67fbfd1381f9f912ad502b40fad54b7d to your computer and use it in GitHub Desktop.
ES6 Destructuring
const student = {
name: 'John Doe',
age: 16,
scores: {
maths: 74,
english: 63
}
};
// We define 3 local variables: name, maths, science
const { name, scores: {maths, science = 50} } = student;
const person = {
name: 'John Doe',
country: 'Canada'
};
// Assign default value of 25 to years if age key is undefined
const { name: fullname, country: place, age: years = 25 } = person;
// Here I am using ES6 template literals
console.log(`I am ${fullname} from ${place} and I am ${years} years old.`);
https://codeburst.io/es6-destructuring-the-complete-guide-7f842d08b98f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment