Last Updated : 11 Jul, 2025
The slice()
method in JavaScript is used to extract a portion of a string and create a new string without modifying the original string.
string.slice(startingIndex, endingIndex);Parameters:
This method uses two parameters. This method does not change the original string.
It returns a part or a slice of the given input string.
Example 1: Slicing String
The code slices the string "Geeks for Geeks" into three parts using the slice() method based on specified indices and logs each part separately.
JavaScript
let A = 'Geeks for Geeks';
b = A.slice(0, 5);
c = A.slice(6, 9);
d = A.slice(10);
console.log(b);
console.log(c);
console.log(d);
Example 2: Negative start or end index case
The code slices the string "Ram is going to school" into multiple parts using the slice() method with various index combinations and logs each part separately.
JavaScript
let A = 'Ram is going to school';
// Calling of slice() Method
b = A.slice(0, 5);
// Here starting index is 1 given
// and ending index is not given to it so
// it takes to the end of the string
c = A.slice(1);
// Here endingindex is -1 i.e, second last character
// of the given string.
d = A.slice(3, -1);
e = A.slice(6);
console.log(b);
console.log(c);
console.log(d);
console.log(e);
Ram i am is going to school is going to schoo going to schoolSupported Browser:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4