JavaScript str.substring() Function

This function gets the characters from a string, between two specified indices, and returns the new string.

Syntax:

str.substring(start, end);

Example: This example uses the parameter (3, 7) for both functions and returns the output. 

Javascript




const str1 = "w3wiki";
const substrResult = str1.substr(3, 7);
const substringResult = str1.substring(3, 7);
 
console.log("Str.substr(3, 7) =", substrResult);
console.log("Str.substring(3, 7) =", substringResult);


Output

Str.substr(3, 7) = ksForGe
Str.substring(3, 7) = ksFo

Difference between substr() and substring() in JavaScript

In JavaScript Both of the functions are used to get the specified part of the string, But there is a slight difference between them. substr() and substring() are string methods used to extract parts of a string. The main difference is that substr() accepts a length parameter, while substring() accepts start and end indices.

Similar Reads

JavaScript str.substr() Function

The str.substr() function returns the specified number of characters from the specified index from the given string....

JavaScript str.substring() Function

...

Difference between substr() and substring()

This function gets the characters from a string, between two specified indices, and returns the new string....