Javascript Object Destructuring (ES6)

In JavaScript, destructuring is when you decompose the properties of an object or the indexes of an array to separate them to create specific variables. This does not mean that these separated objects or arrays can never be used again in the program.

Javascript object destructing

Finish using dot(.) to access object property instead you can use object destructing (ES6)

const events = {
    data: {
        speaker: {
            id: 1,
            name: 'Adama THIAW',
        },
    },
};

const {
    data: {
        speaker: {
            name
        },
    },
} = events;
console.log(name); // display Adama THIAW

Default value of property

We can use default when destructuring object

const events = {
    data: {
        speaker: {
            id: 1,
            name: 'Adama THIAW',
        },
    },
};

const {
    data: {
        speaker: {
            role = 'software developer'
        },
    },
} = events;
console.log(role); // output software developer