diff --git a/README.md b/README.md index 7287b72..6d665f4 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/). For a steady stream of TILs from a variety of rocketeers, checkout [til.hashrocket.com](https://til.hashrocket.com/). -_590 TILs and counting..._ +_591 TILs and counting..._ --- @@ -218,6 +218,7 @@ _590 TILs and counting..._ - [Create An Array Containing 1 To N](javascript/create-an-array-containing-1-to-n.md) - [Create Bootstrapped Apps With Yarn](javascript/create-bootstrapped-apps-with-yarn.md) - [Default And Named Exports From The Same Module](javascript/default-and-named-exports-from-the-same-module.md) +- [Destructuring The Rest Of An Array](javascript/destructuring-the-rest-of-an-array.md) - [Enable ES7 Transforms With react-rails](javascript/enable-es7-transforms-with-react-rails.md) - [Expand Emojis With The Spread Operator](javascript/expand-emojis-with-the-spread-operator.md) - [Freeze An Object, Sorta](javascript/freeze-an-object-sorta.md) diff --git a/javascript/destructuring-the-rest-of-an-array.md b/javascript/destructuring-the-rest-of-an-array.md new file mode 100644 index 0000000..06eecc3 --- /dev/null +++ b/javascript/destructuring-the-rest-of-an-array.md @@ -0,0 +1,22 @@ +# Destructuring The Rest Of An Array + +ES6 offers some amount of pattern matching on arrays. This means you can do +fun stuff like grabbing a couple values and then destructuring the rest of +the array into a variable. + +```javascript +> const kids = ["Mike", "Will", "Dustin", "Lucas", "Eleven", "Max"]; +undefined +> const [first, second, ...rest] = kids; +undefined +> first +"Mike" +> second +"Will" +> rest +["Dustin", "Lucas", "Eleven", "Max"] +``` + +By using the `...` syntax with a variable name in the left-hand side of the +assignment, you are able to capture an array of whatever isn't assigned to +preceding variables.