jashkenas--coffeescript/documentation/sections/slices.md

16 lines
619 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## Array Slicing and Splicing with Ranges
Ranges can also be used to extract slices of arrays. With two dots (`3..6`), the range is inclusive (`3, 4, 5, 6`); with three dots (`3...6`), the range excludes the end (`3, 4, 5`). Slices indices have useful defaults. An omitted first index defaults to zero and an omitted second index defaults to the size of the array.
```
codeFor('slices', 'middle')
```
The same syntax can be used with assignment to replace a segment of an array with new values, splicing it.
```
codeFor('splices', 'numbers')
```
Note that JavaScript strings are immutable, and cant be spliced.