Skip to content

Instantly share code, notes, and snippets.

@syntacticsugar
Created June 13, 2016 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save syntacticsugar/b2fb986b3c1fc580fa72ba1756a33361 to your computer and use it in GitHub Desktop.
Save syntacticsugar/b2fb986b3c1fc580fa72ba1756a33361 to your computer and use it in GitHub Desktop.
The destructuring assignment syntax is a JavaScript expression that makes it possible to extract data from arrays or objects into distinct variables.
var a, b, rest;
[a,b] = [1,2];
console.log(a); // 1
console.log(b); // 2 // wow, so convenient! will I remember to do this, or not bother?
[a, b, ...rest] = [1, 2, 3, 4, 5];
console.log(a); // 1
console.log(b); // 2
console.log(rest); // [3, 4, 5] // incredible... although now I'm thinking this is nearly illegible for other developers
({a, b} = {a:1, b:2})
console.log(a); // 1
console.log(b); // 2
({a, b, ...rest} = {a:1, b:2, c:3, d:4}); //ES7 - not implemented in Firefox 47a01
var x = [1, 2, 3, 4, 5];
var [y, z] = x;
console.log(y); // 1
console.log(z); // 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment