function reverseString(word) {
return word.split("").reverse().join("");
}
//Driver code
let word = "hello";
reverseString(word)
Also, you could even use FOR loop to reverse the string which I am not going to show here.
Using recursion
function reverseString(word) {
//Base code
if(word.length == 0) return "";
return reverseString(word.substr(1)) + word.charAt(0);
}
//Driver code
let word = "hello";
reverseString(word)
//Time Complexity: O(n)
Here, you can see that we're using substr to trim the word for recurring calls.
Check the difference between Recursion and Iteration from here