JavaScript String substring() Method

Substring Javascript

The javascript substring() method returns extracted characters between two given indices of a string and returns the substring. It extracts characters from starting index to the ending index inclusive. It doesn’t modify the original string. In fact, if the start at any time is greater than end the arguments are swapped, and if any of them (start or end) is less than zero, they are treated as zero. 

If fractions are found in place of indices, they’ll be converted to floor values ( integer values less than or equal to a given value).

SYNTAX – string. Substring(starting_Index, ending-Index)

  1. Starting_index – the needed first position. The first character is at index 0 ( 0-based indexing ).
  2. ending_index – the required last position(not including this position). It is optional.

For example-

let str = “techStudy”
a = str.substring(0,4);

b=str.substring(2,5);

c=str.substring(3);

console.log(a);

console.log(b);

console.log(c);

OUTPUT –

 “techS”

“chSt”

“hStudy”

Tags: No tags

Add a Comment

Your email address will not be published. Required fields are marked *