What is the Difference Between Substr and Substring
Both functions take two parameters, but substr() takes the length of the substring to be returned, while substring takes end index (excluding) for a substring.
substr()¶p>The substr() method is used to get a part of the string that starts at the specified index and extends for a specified number of characters afterwards.
Here’s an example of substr() method:
let str = "Welcome to W3Docs"; console.log(str.substr(1,2)); console.log(str.substr(-2,2)); console.log(str.substr(1)); console.log(str.substr(-17,2));
The substring() method is used to get the portion of the string that is between the start and end indexes, or to the end of the string. Here’s the example of substring():
let str = "Welcome to W3Docs"; console.log(str.substring(1,2)); console.log(str.substring(0, 10)); console.log(str.substring(5));