JavaScript String slice() MethodThe JavaScript string slice() method is used to fetch the part of the string and returns the new string. It required to specify the index number as the start and end parameters to fetch the part of the string. The index starts from 0. This method allows us to pass a negative number as an index. In such case, the method starts fetching from the end of the string. It doesn't make any change in the original string. SyntaxThe slice() method is represented by the following syntax: Parameterstart - It represents the position of the string from where the fetching starts. end - It is optional. It represents the position up to which the string fetches. In other words, the end parameter is not included. ReturnPart of the string JavaScript String slice() Method ExampleLet's see some simple examples of slice() method. Example 1Here, we will print the part of the string by passing starting and ending index. Test it NowOutput: vat Example 2Here, we will provide starting index only. In such case, the method fetches the string up to its length. Test it NowOutput: Javatpoint Example 3This is one more example where we provide only the starting index. Test it NowOutput: tpoint Example 4In this example, we will provide negative number as an index. In such case, the method starts fetching from the end of the string. Test it NowOutput: point Example 5In this example, we will provide negative number as a starting and ending index. In such case, the method starts fetching from the end of the string. Test it NowOutput: poin Next TopicJavaScript String |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India