Leading off from my previous post about swapping variable values, I thought I would write a follow-up post on how to reverse a string in Javascript. Surprisingly in Javascript there is no native means of reversing a string (I guess there are very few situations where you would need to do that).
This is not going to be an exercise in code golfing. In-fact, I am only going to show you 3 different ways to reverse a string (although there are potentially tens of different ways to do it). My preference is the first solution using Array.reverse, but pick whatever suits your needs.
This is not going to be about performance or doing the most terse code, this is just a fun little experiment where you might learn a new trick to add to your Javascript toolbox.
Array.reverse:
You’re probably thinking, wait I thought we were reversing a string, why are you using the Array.reverse method. Using the String.split
method we are converting our string into an Array of characters. Then we are reversing the order of each value in the array and then finally we convert the Array back to a String using the Array.join
method.
function reverseString(str) {
return str.split('').reverse().join('');
}
reverseString('dwayne');
Decrementing while-loop:
Although pretty verbose, this solution does have its advantages over solution one. You’re not creating an array and you’re just concatenating a string based on characters from the source string.
From a performance perspective, this one would probably yield the best results (although untested). For extremely long strings, the performance gains might drop out the window though.
function reverseString(str) {
var temp = '';
var i = str.length;
while (i > 0) {
temp += str.substring(i - 1, i);
i--;
}
return temp;
}
reverseString('dwayne');
Recursion
I love how simple and clear this solution is. You can clearly see that the String.charAt
and String.substr
methods are being used to pass through a different value by calling itself each time until the string is empty of which the ternary would just return an empty string instead of using recursion to call itself. This would probably yield the second best performance after the second solution.
function reverseString(str) {
return (str === '') ? '' : reverseString(str.substr(1)) + str.charAt(0);
}
reverseString('dwayne');
Got your own solution? Share it below in the comments
10 different methods with performance tests:
https://jsperf.com/string-reverse-function-performance/6
Appears a for-loop over the string (as a char array) then concatenating to a new string yields the best performance, at least in Chrome:
// decrementing for-loop with concatenation
function reverse(s) {
var o = '';
for (var i = s.length - 1; i >= 0; i--)
o += s[i];
return o;
}
Amazingly, the most naive method performs best (as in, it’s probably the code you’d get from a naive programmer).
Dear Dwayne,
Nice concept !!
Is there any pre-defined function in JavaScript for do this thing easily?
Thanks
Dear Dwayne,
pls Explain reverse string without using reverse method() ?
Send for my mail ,pls ..
Waiting egerly for your positive reply….
thank you