Created
September 10, 2017 03:43
-
-
Save StevenJL/089a7dd391b4145ef68c0bbbcf9d0463 to your computer and use it in GitHub Desktop.
Destructuring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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