Appearance
What is the Difference Between Substr and Substring
There are two String methods in JavaScript, namely, substr() and substring(), that are used to extract a portion of a string. However, there are slight differences between substr() and substring() methods.
Both methods can take two parameters, but substr() uses a start index and a length, while substring() uses a start index and an end index (excluding).
substr()
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 the substr() method:
Javascript substr method
Output appears here after Run.
substring()
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 an example of the substring() method:
Javascript substring method
Output appears here after Run.
Note that substring() treats negative indices as 0, unlike substr() which counts from the end of the string.
Though substr() is not strictly a deprecated function, it is considered a legacy feature and you should avoid using it whenever possible. It is officially deprecated in the ECMAScript specification and may be removed in the future. So, you can use the substring() method instead.