JavaScript | string.slice()

The string.slice() is an inbuilt function in javascript which is used to return a part or slice of the given input string.

string.slice(startingindex, endingindex)

Parameter: This function uses two parameter startingindex( from which index, string should be started) and endingindex (before which index, string should be included) .
Return Values: It returns a part or a slice of the given input string.

JavaScript code to show the working of the string.slice() function:

Code #1:

   // Taking a string as input.
   var A = 'Ram is going to school';
   // Calling of slice() function.
   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);
   document.write(b +"<br>");
   document.write(c +"<br>");
   document.write(d +"<br>");


Ram i
am is going to school
is going to schoo
going to school

This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment



load comments

Subscribe to Our Newsletter