Hello! First post here, hopefully I'm doing it right.
We got the Array.prototype.reverse
method a while ago, which reverses an array in place as well as returning the result. This is a useful feature, and one that I think would be useful for strings.
Currently implementing this in a library (naively) would look something like:
String.prototype.reverse = function() {
return this.split("").reverse().join("");
};
This does not reverse the string in place, instead returning the result. This makes sense given that strings are primitives.
This seems like it would be a relatively easy feature to implement, with a fairly low chance of breaking any existing code. The advantages of having this as a builtin are:
- It will likely be faster than the naive implementation above
- It will reduce the number of external libraries or custom functions required, making code shorter and/or cleaner
- It would make sense for strings to have a reverse method if arrays do, given that it's a fairly common operation for both data types
Interested to see if others have noticed this, or think it's a good idea!