JavaScriptStrings

String.prototype.slice()

Returns a portion of a string from a start index to an end index (not included). Supports negative indexes.

Returns
A new string with the extracted piece, or an empty string when the range contains no characters.
Changes the original
No
On this page

Example

#
JavaScript
const title = 'JavaScript in minutes'

console.log(title.slice(0, 10))
console.log(title.slice(11, 13))
console.log(title.slice(-7))
console.log(title)
Console
JavaScript
in
minutes
JavaScript in minutes

Definition and usage

#

The slice() method cuts out a piece of text and returns it as a new string. The first argument is the index where you start, and the second is the index where you stop, with the character at that index excluded from the result. Without a second argument, the slice reaches to the end of the string.

Negative indexes count from the end: -1 is the last character, -3 is the third from the end. That makes text.slice(-4) a convenient way to grab the last four characters, for example the tail end of an order number.

The original string doesn't change, because strings in JavaScript are immutable. When the start falls after the end, the result is an empty string. In new code, slice() is usually the better choice over substring(), because it works the same way as slice() on arrays.

Syntax

#
Syntax
string.slice(indexStart)
string.slice(indexStart, indexEnd)

Parameters

#
  • indexStart

    number

    The index of the first character of the slice. A negative value counts from the end of the string.
  • indexEnd

    number, optional

    The index before which the slice ends (that character is not included in the result). Defaults to the length of the string. Can be negative.

More examples

#
File extension and masking a number
JavaScript
const file = 'final-project.html'
const extension = file.slice(file.lastIndexOf('.') + 1)

const orderId = 'ORD-2026-004815'
const masked = '*'.repeat(orderId.length - 4) + orderId.slice(-4)

console.log(extension)
console.log(masked)
Console
html
***********4815
Capitalizing the first letter
JavaScript
function capitalize(word) {
  return word.slice(0, 1).toUpperCase() + word.slice(1)
}

console.log(capitalize('paris'))
console.log(capitalize('javaScript'))
Console
Paris
JavaScript

Unlike word[0], calling slice(0, 1) on an empty string returns "" instead of undefined, so the function won't throw an error.

Indexes outside the string
JavaScript
const code = 'CSS'

console.log(code.slice(1, 100))
console.log(code.slice(5).length)
console.log(code.slice(2, 1).length)
Console
SS
0
0

Indexes that are too large are clamped to the string's length, and when the start falls after the end, you get an empty string. slice() does not swap its arguments, unlike substring().

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • To grab the last few characters, use a negative index: text.slice(-3) instead of text.slice(text.length - 3).
  • Remember that the end index is not included in the result. The length of the slice is indexEnd - indexStart.
  • When slicing text that contains emoji, you might cut a character in half, because indexes count UTF-16 units. In that case, turn the string into an array of characters first: [...text].slice(0, 5).join("").

Related entries

#

Spotted a mistake or missing an example? Write to us.