De-structuring Flashcards
What is destructuring, conceptually?
Assigning a property of an object to a variable.
variable name of destructuring should match property name of object pulling info from.
What is the syntax for Object destructuring?
const {property : variable} = object
if variable has same name as property, just write the name.
able to assign value of properties. able to give a property an unassigned property value. person = { currentAge:28 } {currentAge: age = 18 } = person
What is the syntax for Array destructuring?
const [variable at index] = array
let [x, y, z] = getScores();
console. log(x); // 70
console. log(y); // 80
console. log(z); // 90
How can you tell the difference between destructuring and creating Object/Array literals?
creating curly brackets right side
destructuring curly brackets left side
How to destructure a null object
let { firstName, lastName } = getPerson() || {};
use the or operator, firstName, lastName will return undefined.