Skip to content

Instantly share code, notes, and snippets.

@zpao
Created February 26, 2011 00:45
Show Gist options
  • Save zpao/844787 to your computer and use it in GitHub Desktop.
Save zpao/844787 to your computer and use it in GitHub Desktop.
Multiple (destructuring) assignment in JS
var arr = ["yea, yea", "we knew this worked"];
var [a, b] = arr;
// a == "yea, yea"
// b == "we knew this worked"
var obj = {
foo: "WTF",
bar: "this works?",
baz: "looks like it"
};
var { foo: c, bar: d} = obj;
// c == "WTF"
// d == "this works?"
var obj2 = {
foo: { dude: "WTF" },
bar: "this works too?",
baz: "looks like it"
};
var { foo: e, bar: f} = obj2;
// e == { dude: "WTF" }
// d == "this works too?"
// using the unchanged obj2 from above...
var { foo: { dude: g } } = obj2;
// g == "WTF"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment