Skip to content

Instantly share code, notes, and snippets.

@StevenJL
Created September 10, 2017 03:43
Show Gist options
  • Save StevenJL/089a7dd391b4145ef68c0bbbcf9d0463 to your computer and use it in GitHub Desktop.
Save StevenJL/089a7dd391b4145ef68c0bbbcf9d0463 to your computer and use it in GitHub Desktop.
Destructuring
// Array Matching
var list = [1, 2, 3]
var [a, , b] = list
a // => 1
b // => 3
[b, a] = [a, b]
a // => 3
b // => 1
// Object Matching
const obj = {a: 2, b: 3, c: 4}
var { a, b, c} = obj
a // => 2
b // => 3
c // => 4
// You can use default values when destructuring
var obj = {a: 1}
var {a, b = 2 } = obj
a // => 1
b // => 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment