Five Ways to Reverse a String in Javascript

Five Ways to Reverse a String in Javascript

Image for post

?Write a function to reverse string? is one of the most obvious algorithms questions that you can expect to be asked at a JavaScript interview, especially if you are just starting your career as a software engineer.

This blog is going to present five different solutions for this challenge.

1. For loop

The most straightforward ? and one might say naive approach ? is to utilise a for loop. In this approach we can use decrementing or incrementing loop to iterate through each letter of the string and create a new reversed string:

function reverse(str){ let reversed = “”; for (var i = str.length – 1; i >= 0; i–){ reversed += str[i]; } return reversed;}

ES6, however, introduced a new for loop syntax, such as for?of. It eliminates the possibility of making lots of silly typos while declaring our for loop and results in a much neater piece of code:

function reverse(str){ let reversed = “”; for(let char of str){ reversed = char + reversed; } return reversed;}

2. reverse() Method for Arrays

When your interviewer didn?t specifically ask you to avoid using a built-in reverse() method, you should definitely take advantage of it. In JavaScript, the reverse() method exists only for arrays, so first we need to use split() to transform the string into an array. Then apply the reverse() method and finally join() it all back together:

function reverse(str){ return str.split(“”).reverse().join(“”);}

3. Spread Syntax (ES6) + reverse() Method for Arrays

ES6 introduces one more way for splitting our string into an array, and that is a spread operator [?]. Even though this solution is similar to the previous one, I believe it?s worth mentioning, because it looks and works pretty great.

function reverse(str){ return […str].reverse().join(”);}

4. reduce() Method for Arrays

One of the most unconventional approaches that I rarely see in reverse string discussions is using the reduce() method. Once again, it only works for arrays, so first we need to split our string. Then we accumulate our values into an empty string that becomes a reverse of our original string by the end of this operation:

function reverse(str){ return str.split(“”).reduce((rev, char)=> char + rev, ”); }

5. Recursion

And the last, but most certainly not the least approach to solving the reverse string problem, is recursion. We are making the function call itself string.length until it hits our base case, an empty string. We chop the first character of the string off using substr() method and then add it to the end of the string.

function reverse(str){ if(str === “”){ return str }else{ return reverse(str.substr(1)) + str[0] }}

One way to refactor our recursion would be to use a ternary operator:

function reverse(str){ return str ? reverse(str.substr(1)) + str[0] : str}

Neat-o!

So now that you learned five cool ways to reverse a string, you will be able to impress your interviewer by discussing various ways to ninja kick this challenge!

1

No Responses

Write a response