JavaScript SubString

The JavaScript SubString string function is used to pick the part of a string and return in a new string. JavaScript SubString function will accept two integer values. The first integer value is the index position where the extraction starts, and the second index position is where the removal ends.

JavaScript SubString Syntax

The syntax of the JavaScript SubString function is:

String_Object.substring(Start, End)
  • String_Object: Valid string from which you want to extract.
  • Start: Index position from where you want to extract.
  • End: Please specify the index position of the Endpoint. If you ignore it, the SubString function starts from the first index position and continue to the last.


  • If the second index value higher than the first index, the SubString function will swap two arguments.
  • And, If you are using the Negative numbers as an argument, the SubString function will consider it as zero.

JavaScript SubString Function Example

It will help you understand the String SubString in JS.

<!DOCTYPE html>
    <title> SubString JavaScript </title>
    <h1> JavaScript SubString </h1>
    var Str1 = "Tutorial Gateway";
    var Str2 = Str1.substring(2, 13);
    var Str3 = Str1.substring(0, 13);
    var Str4 = Str1.substring(2);
    var Str5 = Str1.substring(13, 2);
    document.write(Str2 + "<br \>");
    document.write(Str3 + "<br \>");
    document.write(Str4 + "<br \>");

JavaScript SUBSTRING Function

The following JavaScript code extracts the original string, starting at index position 2 and ends at index position 12

var Str2 = Str1.substring(2, 13);

Next, We ignored the second argument, which means JS SubString function will start at index position 2 and ends at last.

var Str4 = Str1.substring(2);

Next, We specified the first index as 13 and second index as 2. SubString function will swap two arguments which means the it is equal to Str1.substring(2, 13)

var Str5 = Str1.substring(13, 2);