DEV Community

Randy Rivera
Randy Rivera

Posted on • Updated on

Reversing a String

  • Let's Reverse the provided string.
  • You may need to turn the string into an array before you can reverse it.
  • Your result must be a string.
function reverseString(str) {
 return str;

Enter fullscreen mode Exit fullscreen mode
  • Answer:
function reverseString(str) {
  let strArr = str.split("");
  let reverseStrArr = strArr.reverse();
  let reverseStr = reverseStrArr.join("");
  return reverseStr;

console.log(reverseString("hello")); // will display olleh
Enter fullscreen mode Exit fullscreen mode


  • Our goal is to take the input, str, and return it in reverse. Our first step is to split the string by characters using split(""). Notice that we don’t leave anything in between the single quotes, this tells the function to split the string by each character.
  • Using the split() function will turn our string into an array of characters, keep that in mind as we move forward.
  • Next we chain the reverse() function, which takes our array of characters and reverses them.
  • Finally, we chain join("") to put our characters back together into a string. Notice once again that we left no spaces in the argument for join, this makes sure that the array of characters is joined back together by each character.


function reverseString(str) {
  return str.split("").reverse().join("");
Enter fullscreen mode Exit fullscreen mode
  • just simply return it.

Top comments (0)