The slice() function is a string function of Node.js which is used to extract sub-string from a string.
Syntax:
string.slice( start, end )
Parameters: This function uses three parameters as mentioned above and described below:
- string: It holds the string content. The substring is extracted from this string.
- start: This parameter holds the starting index of the sub-string.
- end: This parameter holds the ending index of the sub-string.
Return type: The function returns the substring.
The program below demonstrates the working of the function:
Program 1:
javascript
function findsubstr(str) {
const index = str.slice(12, 25);
console.log(index);
}
const str = "Welcome to GeeksforGeeks" ;
findsubstr(str);
|
Output:
GeeksforGeeks
Program 2:
javascript
function findsubstr(str, start, end) {
const index = str.slice(start, end);
console.log(index);
}
const str = "Welcome to GeeksforGeeks" ;
const start = 0;
const end = 6;
findsubstr(str, start, end);
|
Output:
Welcome
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
30 Mar, 2023
Like Article
Save Article