Write a function to reverse string’ is one of the most obvious algorithms information especially if you are just commence on JavaScript.

In the following 3 (underlined) ways have been mentioned for the process-

Reverse method for Arrays

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(“”);

}

Recursion

It is the most certain approach to solving the reverse string issue. We are making the function call itself string.length until it hits our base case, an empty string. We remove 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]

}

}

Reduce Method of Arrays

It is one of the most unconventional approaches to reverse string. 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, ”);

}