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.

Description: Extracts a portion of a string from the start index to the end index.

Example: 'banana bread'.slice(0, 6) => 'banana'

Want to go further? Chain Slice with other functions in the visual Playground — pipe one output into the next and watch your data transform.

Related functions

© 2026 Heifara Buval