Friday, February 24, 2017

ES6: Destructing

Destructuring allows us to access properties of an object and save them in variables all at once

const person={first:”A”,second:”B”,third:”C”};
To save these values, we would have had to write something like
function saveValues(){
const first=person.first;
const second=person.second;
const third=person.third;
}

const {first,second,third} = person;

console.log(first,second,third); //gives us A B C

We can rename the containers of values retrieved from objects or arrays too
const {first:one,second:two,third:three}=person;

We can also assign default values

const {first:one=“A”,second:two=“A”,third:three=“C”}=person;

No comments:

Post a Comment