Skip to content

Instantly share code, notes, and snippets.

@prof3ssorSt3v3
Created July 25, 2017 02:07
Show Gist options
  • Save prof3ssorSt3v3/f2c26e4c9e66cd417d980879271e859f to your computer and use it in GitHub Desktop.
Save prof3ssorSt3v3/f2c26e4c9e66cd417d980879271e859f to your computer and use it in GitHub Desktop.
// ES6 Destructuring
//extracting values from objects and arrays
//and assigning them to multiple variables
let name, id, nm, num, star, planet, rest;
let personObj = {name:"Arthur Dent", id:42, planet:'Earth'};
let personArr = ["Zaphod", 123, 'Betelgeuse'];
//[nm, num] = personArr;
//console.log(nm, num);
//[nm, ,star] = personArr;
//console.log(nm, star);
//[nm, ...rest] = personArr;
//console.log(nm, rest.length, rest[1]);
//({planet, name} = personObj);
//console.log(name, planet);
const f = ({id, planet, name, star='Sol'}) => {
console.log(name, id, planet, star);
}
f(personObj);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment