There are cases where you have to reverse a string, be it a coding interview or an actual case in your work. So, given a string input, how can you reverse a string in JavaScript?
Inputs and Outputs example :
- Input : JavaScript, Output : tpircSavaJ
- Input : reverse, Output : esrever
- Input : string, Output : gnirts
Solution
There is a built-in reverse function in JavaScript, but sadly it is only for arrays.
So, we have to first convert our string to an array, reverse it and convert it back to a string.
How do we convert it?
We use split()
and join()
of course. Don’t forget to chain them with reverse()
.
function reverseString(str) {
return str.split('').reverse().join('');
}
Now, we’re gonna dive deeper into the code.
As you can see, we’re utilizing 3 built-in functions, let’s discuss each one.
(One thing to note though, the functions are self explanatory in itself, so forgive me if the explanations are a bit awkward)
split('')
splits your string into an array. Since we’re passing an empty string as its parameter, each character is made into an element. If we have 'good'
for example, it will be splitted into ['g','o','o','d']
.
reverse()
reverses your array. Since it only works on arrays, we turn the string into an array in the previous step. As an example, if we reverse ['g','o','o','d']
, we’ll get ['d','o','o','g']
.
join('')
combines your array elements back into a string. The parameter indicates what we want to separate the elements with, in this case, we pass an empty string so they will be joined with no separators in between them. As an example, if we join ['d','o','o','g']
, we’ll get 'doog'
.
Finally, chaining it all together will give us our expected result with a clean one-liner code solution.
Time Complexity : O(n)
Each of the built-in function iterates through each character of the string, which means, it iterates n times or more precisely 3n times. That’s why it has O(n) time complexity.
Space Complexity : O(n)
When we convert the string into an array using split()
, we’re making a new array with the elements’ amount equivalent to the string length which is n. That’s why it has O(n) space complexity.
Conclusion
That’s the simplest and cleanest method to reverse a string in JavaScript.
You just use 3 built-in functions, split()
, reverse()
and join()
.
You can surely explore other methods, if you find better solutions, please comment below!
If you want another explanation on string topic in JavaScript, check out How to Remove Spaces from String in JavaScript and How to Check for a Palindrome in JavaScript?.
For you who want to brush up your JavaScript skills, whether to deepen or learn form zero, I recommend you try Codecademy. There are tons of JavaScript lessons that you can learn in a structured and interactive way. You can learn from the most basic materials to advanced stuffs and even preparing for technical interviews. Go try it now!
Check out the rest of my blog for more helpful contents on Data Structures and Algorithms in JavaScript!
See you next post!