loading
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const taxPayer = {
    	firstName: 'Ian', lastName: 'DesJardins',
    	ssn: '123-45-6789', pastDue: true,
};

console.log('Object destructuring example');
// destructure the taxPayer object into new variables
const { firstName, lastName, ssn, pastDue } = taxPayer;

console.log('firstName =', firstName);
console.log('lastName =', lastName);
console.log('ssn =', ssn);
console.log('pastDue =', pastDue, '\n');

// may also be used to destructure an object for a function's parameter
function parseTaxPayer({firstName, lastName, ssn, pastDue}) {
    console.log('inside parseTaxPayer()');
    console.log('firstName =', firstName);
    console.log('lastName =', lastName);
    console.log('ssn =', ssn);
    console.log('pastDue =', pastDue);
}

parseTaxPayer(taxPayer);


console.log('\nObject key access example')
// this is the equivalent of destructuring
const firstNameAgain = taxPayer.firstName;
const lastNameAgain = taxPayer.lastName;
const ssnAgain = taxPayer.ssn;
const pastDueAgain = taxPayer.pastDue;
console.log('firstNameAgain =', firstNameAgain);
console.log('lastNameAgain =', lastNameAgain);
console.log('ssnAgain =', ssnAgain);
console.log('pastDueAgain =', pastDueAgain, '\n');