String.prototype.slice
Extracts a section of a string and returns it as a new string, without modifying the original string
Syntax
string.slice(indexStart, indexEnd?)Parameters
| Parameter | Type | Description |
|---|---|---|
| indexStart | number | Zero-based index at which to begin extraction |
| indexEnd | number | Zero-based index before which to end extraction |
Return Value
A new string containing the extracted section
Examples
const str = 'Hello, World!';
console.log(str.slice(7)); // 'World!'
console.log(str.slice(0, 5)); // 'Hello'const str = 'JavaScript';
console.log(str.slice(-6)); // 'Script'
console.log(str.slice(-6, -1)); // 'Scrip'const email = '[email protected]';
const domain = email.slice(email.indexOf('@') + 1);
console.log(domain); // 'example.com'Understanding String.prototype.slice
The String.prototype.slice method in JavaScript extracts a section of a string and returns it as a new string, without modifying the original string. It belongs to the String object and is one of the most widely used methods for working with string values in modern JavaScript and TypeScript applications.
The method signature is string.slice(indexStart, indexEnd?). It accepts 2 parameters: indexStart, indexEnd. When called, it returns a new string containing the extracted section. Understanding when and how to use slice() helps you write more expressive, readable code.
Common use cases for String.prototype.slice include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like string-substring, string-split, array-slice, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for String.prototype.slice is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
String.prototype.substringReturns the part of the string from the start index up to and excluding the end index
String.prototype.splitDivides a string into an ordered list of substrings and returns them as an array
Array.prototype.sliceReturns a shallow copy of a portion of an array into a new array object selected from start to end (end not included)
More String Methods
Other methods in the String object
Related Tools
More String Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.