Why? Because convenience.
Presumably. There's no way to know for sure without asking the original authors of this decision.
But we can only assume that it's based on reasoning similar to that which led to having e.g. functions Left and Right in Visual Basic, even though identical results can be obtained using the Mid function with a different combination of arguments, albeit less conveniently and with more or longer lines of code. (Or, pick own analogy in own favorite language.)
Fox example, using the length
argument of substr
instead of the end
argument of substring
can lead to some mild readability/conciseness gains:
var test = "First Second Last";
//If you want "Second", do either of:
alert(test.substring(test.indexOf("Second"), test.indexOf("Second") + 'Second'.length))
alert(test.substr(test.indexOf("Second"), "Second".length)) //<------ WOW!! ---------->
Alternatively, when using substring
, you could store test.indexOf("Second")
in a temp variable to shorten the substring
call:
var n = test.indexOf("Second");
alert(test.substring(n, n + 'Second'.length))
but that's one more line of code and one more temp that you don't need when using substr
.
Bottom line: It's really no big deal! They added one more function for slightly more convenience.