// In an array destructuring from an array of length N specified on the right-hand side of the assignment, if the number of variables specified on the left-hand side of the assignment is greater than N, only the first N variables are assigned values. The values of the remaining variables will be undefined.const foo =['one','two'];const[red, yellow, green, blue]= foo;console.log(red);// "one"console.log(yellow);// "two"console.log(green);// undefinedconsole.log(blue);//undefined
const arrValue =['one','two','three'];// destructuring assignment in arraysconst[x, y, z]= arrValue;console.log(x);// oneconsole.log(y);// twoconsole.log(z);// three
/*
* On the left-hand side, you decide which values to unpack from the right-hand
* side source variable.
*
* This was introduced in ES6
*/const x =[1,2,3,4,5]const[a, b]= x
console.log(a)// prints out: 1console.log(b)// prints out: 2
/*
Array Destructuring: The following example shows us how to convert all
the elements of an array to a variable.
Object Destructuring: The following example shows us how to convert all
the properties of an object into a variable.
*///Array Destructuringconst friends =['Bill','Gates'];const[firstName, secondName]= friends;console.log(secondName);//Gates//Object Destructuringconst person ={name:"Bill Gates",age:17,phone:"123456789"};const{name}= person;console.log(name);//Bill Gates
// before you would do something like thisconst person ={name:'Sara',age:25,gender:'female'}let name = person.name;let age = person.age;let gender = person.gender;console.log(name);// Saraconsole.log(age);// 25console.log(gender);// female