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
Example
#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)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
#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
#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)function capitalize(word) {
return word.slice(0, 1).toUpperCase() + word.slice(1)
}
console.log(capitalize('paris'))
console.log(capitalize('javaScript'))Unlike word[0], calling slice(0, 1) on an empty string returns "" instead of undefined, so the function won't throw an error.
const code = 'CSS'
console.log(code.slice(1, 100))
console.log(code.slice(5).length)
console.log(code.slice(2, 1).length)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 oftext.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
#- String.prototype.substring()Returns a portion of a string between two indexes. Negative values are treated as 0, and reversed indexes are swapped.
- String.prototype.at()Returns the character at the given index. Negative indexes count from the end, so at(-1) is the last character.
- String.prototype.indexOf()Returns the index of the first occurrence of the given text in a string, or -1 when it is not there.
- String.prototype.split()Splits a string into an array of pieces based on a separator: a character, a string, or a regular expression.
- Array.prototype.slice()Returns a copy of part of an array without changing the original.
Spotted a mistake or missing an example? Write to us.