Destructuring assignments helps to reduce the no of lines of code. Its basically sugarcoating
Destructuring assignment for arrays
//declare an array var arr =["Prathap","H","Kudupu"] //assign the content of the array to the variables var firstName=arr[0]; var MI=arr[1]; var lastName=arr[2]; console.log(firstName,MI,lastName); //Same code in es 6 using destructuring assignment for arrays var [esfirstName,esMI,eslastName]=arr; console.log(esfirstName,esMI,eslastName);
output
C:\Development\NodeProjects\vanilanode>node destructingAssignment.js Prathap H Kudupu Prathap H Kudupu
Destructuring assignment for objects
//declare and oject in an array var person= { firstName:"Atharv", MI:"P", lastName:"Kudupu" }; //assign object properties to variables var first= person.firstName; var mi= person.MI; var last= person.lastName; //display in console console.log(first,mi,last); //Assign object properties using es6 var {firstName,MI,lastName}=person; //display in console console.log(firstName,MI,lastName);
C:\Development\NodeProjects\vanilanode>node destructingAssignment.js Atharv P Kudupu Atharv P Kudupu
No comments:
Post a Comment