JavaScript Slice
slice() extracts a section of a string between a start and (optional) end index and returns it as a new string. Indexes are zero-based, and negative numbers count from the end. Its killer feature.
Syntax
str.slice(start, end) Examples
| Input | Arguments | Output |
|---|---|---|
| "hello" | 0, 3 | "hel" |
| "hello" | -2 | "lo" |
| ↳ Negative index counts from the end. | ||
When to use it
- Take a prefix, suffix or fixed-width slice of text.
- Trim file extensions or known prefixes.
Gotchas
- end is exclusive: slice(0, 3) returns 3 characters, indexes 0–2.
Try it live
Type your input and see Slice transform it instantly.
Want to go further? Chain Slice with other functions, pipe one output into the next and watch your data transform in the visual Playground.