Destructuring Assignment with Object in ECMAScript

Create new Javascript file named demo1.js. This file contains code demo as below:

let person = { firstName: 'John', middleName: 'Smith', lastName: 'Thomas' };
let { firstName, lastName, middleName } = person;
console.log('First Name: ' + firstName);
console.log('Middle Name: ' + middleName);
console.log('Last Name: ' + lastName);

Use node demo1.js statement run code demo

First Name: John
Middle Name: Smith
Last Name: Thomas




Create new Javascript file named demo2.js. This file contains code demo as below:

let animal = {
    dog: 'Dog 1',
    cat: {
        bigger: 'Cat 1',
        smaller: 'Cat 2'
    },
    turtle: 'Turtle 1'
};

const { dog, turtle } = animal;
console.log(dog + ', ', turtle);

const { bigger, smaller } = animal.cat;
console.log(bigger + ', ' + smaller);

Use node demo2.js statement run code demo

Dog 1,  Turtle 1
Cat 1, Cat 2