Javatpoint Logo
Javatpoint Logo

JavaScript String substring() Method

The JavaScript string substring() method fetch the string on the basis of provided index and returns the new sub string. It works similar to the slice() method with a difference that it doesn't accepts negative indexes. This method doesn't make any change in the original string.

Syntax

The substring() method is represented by the following syntax:

Parameter

start - 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.

Return

Part of the string.

JavaScript String substring() Method Example

Let's see some simple examples of substring() method.

Example 1

Let's see a simple example to print the part of the string.

Test it Now

Output:

Java

Example 2

In this example, we will fetch single character from the string.

Test it Now

Output:

t

Example 3

In this example, we will provide only the starting index.

Test it Now

Output:

point

Example 4

In this example, we will not pass any parameter with the method. In such case, the method returns the complete string.

Test it Now

Output:

Javatpoint

Next TopicJavaScript String




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA