This is one of the most common interview question asked during interview for testing your understanding on data structure and algorithms.


Let's write an algorithm to reverse a given string using recursion in javascript


let reversestr = str => {

    if(str.length == 1){
      return str[0];
    return str[str.length-1] + reversestr(str.substr(0,str.length-1));

const str = reversestr("malayalam");
console.log("result of reversed string is: ", str); 


Algorithm explanation

Here reversestr is an arrow function from javascript es6 which accepts a string as an input i.e "malayalam" and returns a reversed string which is same.

#3  is the base condition for the recursion to stop,  base condition is the most important part of recursion.

#6 concatenates the last character from the string and recursively calls the function by removing the last character from the string.