What is destructuring

Destructuring allows you to easily extract values from Objects or Arrays and assign to new values or even assign default values to undefined properties

Imagine this code :

user = { name: ‘dave’, gender: ‘male’, age: ’24’ }

name   = user.name

gender = user.gender

age    = user.age

We can replace it with single line of code

user = { name: ‘dave’, gender: ‘male’, age: ’24’ }

const { name, gender, age } = user

Similarly we have array destructuring:

colours = [“red”, “blue”, “green”]

const [first, second, third] = colours

Uday Ogra

Connect with me at http://facebook.com/tendulkarogra and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *