Destructuring is a better way to get the values from array and objects with a concise code. let’s deep dive into it.
Let’s suppose you have the following object:
const FullName = {
firstName: 'Muhammad',
lastName: 'Awais'
}
in the normal scenario, to get the firstname and lastname from the object, you have to create new variables and assign the object values in them, like below:
let firstName = FullName.firstName // Muhammad
let lastName = FullName.lastName // Awais
While in using destructuring you can do above thing in a most better way:
let { firstName, lastName } = FullName
console.log(firstName) // Muhammad
console.log(lastName) // Awais
behind the scenes, {} are telling javascript to generate the aforementioned variables and assign the object values in it, es6 power is used here.
Top comments (0)