How to Reverse a String in JavaScript: 23 Ways

Facebook logoTwitter logoLinkedin logo
image contains "How to Reverse a String in JavaScript" text

1. Using a for loop:

Explanation: Iterate through the string in reverse and build a new reversed string.

function reverseString(str) {
  let reversed = "";
  for (let i = str.length - 1; i >= 0; i--) {
    reversed += str[i];
  }
  return reversed;
}
const reversedString = reverseString("Hello");
console.log(reversedString); // olleH
Copy

2. Using the split(), reverse(), and join() methods:

Split the string into an array, reverse the array, and then join it back to a string.

function reverseString(str) {
  return str.split("").reverse().join("");
}
const reversedString = reverseString("Hello");
console.log(reversedString); // olleH
Copy

3. Using recursion:

Recursively swap the first and last characters of the string until it's reversed.

function reverseString(str) {
  if (str === "") return "";
  else return reverseString(str.substr(1)) + str[0];
}
const reversedString = reverseString("Hello");
console.log(reversedString); // "olleH"
Copy

4. Using the reduce() method:

Use the reduce() method to build the reversed string.

function reverseString(str) {
  return str.split("").reduce((reversed, char) => char + reversed, "");
}
const reversedString = reverseString("Hello");
console.log(reversedString); // "olleH"
Copy

5. Using the map() and reverse() methods:

Use the map() method to create an array of reversed characters and then join them.

function reverseString(str) {
  return str.split("").map((char, index, arr) => arr[arr.length - 1 - index]).join("");
}
const reversedString = reverseString("Hello");
console.log(reversedString); // "olleH"
Copy

6. Using the substring() method:

Build the reversed string by taking substrings from the original string in reverse order.

function reverseString(str) {
  let reversed = "";
  for (let i = 0; i < str.length; i++) {
    reversed = str.charAt(i) + reversed;
  }
  return reversed;
}
const reversedString = reverseString("Hello");
console.log(reversedString); // "olleH"
Copy

7. Using the split() method with a spread operator:

Convert the string to an array and use the spread operator to reverse it.

function reverseString(str) {
  return [...str].reverse().join("");
}
const reversedString = reverseString("Hello");
console.log(reversedString); // "olleH"
Copy

8. Using the Array.from() method:

Use the Array.from() method to create an array from the string and then reverse it.

function reverseString(str) {
  return Array.from(str).reverse().join("");
}
const reversedString = reverseString("Hello");
console.log(reversedString); // "olleH"
Copy

9. Using the Array.from() method with reduceRight():

Convert the string to an array, use reduceRight() to reverse it and join the characters.

function reverseString(str) {
  return Array.from(str).reduceRight((reversed, char) => reversed + char, "");
}
const reversedString = reverseString("Hello");
console.log(reversedString); // "olleH"
Copy

10. Using a while loop:

Create a reversed string character by character using a while loop.

function reverseString(str) {
  let i = str.length - 1;
  let reversed = "";
  while (i >= 0) {
    reversed += str[i];
    i--;
  }
  return reversed;
}
const reversedString = reverseString("Hello");
console.log(reversedString); // "olleH"
Copy

11. Using the XOR swap algorithm:

Swap characters using the XOR operation.

function reverseString(str) {
  let arr = str.split("");
  let start = 0;
  let end = str.length - 1;
  while (start < end) {
    arr[start] = String.fromCharCode(arr[start].charCodeAt(0) ^ arr[end].charCodeAt(0));
    arr[end] = String.fromCharCode(arr[start].charCodeAt(0) ^ arr[end].charCodeAt(0));
    arr[start] = String.fromCharCode(arr[start].charCodeAt(0) ^ arr[end].charCodeAt(0));
    start++;
    end--;
  }
  return arr.join("");
}
const reversedString = reverseString("Hello");
Copy

12. Using a for...of loop:

Iterate over the characters of the string using a for...of loop.

function reverseString(str) {
  let reversed = "";
  for (const char of str) {
    reversed = char + reversed;
  }
  return reversed;
}
const reversedString = reverseString("Hello");
console.log(reversedString); // "olleH"
Copy

13. Using charAt() and a for loop:

Iterate through the string using a for loop and append characters using charAt().

function reverseString(str) {
  let reversed = "";
  for (let i = 0; i < str.length; i++) {
    reversed = str.charAt(i) + reversed;
  }
  return reversed;
}
const reversedString = reverseString("Hello");
console.log(reversedString); // "olleH"
Copy

14. Using recursion and substring():

Recursively shorten the string using substring() and append the first character.

function reverseString(str) {
  let reversed = "";
  for (let i = 0; i < str.length; i++) {
    reversed = str.charAt(i) + reversed;
  }
  return reversed;
}
const reversedString = reverseString("Hello");
console.log(reversedString); // "olleH"
Copy

15. Using a for...in loop:

Iterate through the string using a for...in loop (not recommended for strings).

function reverseString(str) {
  let reversed = "";
  for (let index in str) {
    reversed = str[index] + reversed;
  }
  return reversed;
}
const reversedString = reverseString("Hello");
console.log(reversedString); // "olleH"
Copy

16. Using a do...while loop:

Build the reversed string using a do...while loop.

function reverseString(str) {
  let i = str.length - 1;
  let reversed = "";
  do {
    reversed += str[i];
    i--;
  } while (i >= 0);
  return reversed;
}
const reversedString = reverseString("Hello");
console.log(reversedString); // "olleH"
Copy

17. Using the split() method and a for loop:

Split the string into an array and use a for loop to reverse it.

function reverseString(str) {
  const arr = str.split("");
  let reversed = "";
  for (let i = arr.length - 1; i >= 0; i--) {
    reversed += arr[i];
  }
  return reversed;
}
const reversedString = reverseString("Hello");
console.log(reversedString); // "olleH"
Copy

18. Using a stack data structure:

Push each character onto a stack and then pop them to build the reversed string.

function reverseString(str) {
  const stack = [];
  for (const char of str) {
    stack.push(char);
  }
  let reversed = "";
  while (stack.length > 0) {
    reversed += stack.pop();
  }
  return reversed;
}
const reversedString = reverseString("Hello");
Copy

19. Using String.prototype.slice() with a for loop:

Iterate through the string in reverse order using slice() to extract characters.

function reverseString(str) {
  let reversed = "";
  for (let i = str.length - 1; i >= 0; i--) {
    reversed += str.slice(i, i + 1);
  }
  return reversed;
}
const reversedString = reverseString("Hello");
console.log(reversedString); // "olleH"
Copy

20. Using a for...of loop and unshift():

Iterate through the string with a for...of loop and prepend characters using unshift().

function reverseString(str) {
  const reversed = [];
  for (const char of str) {
    reversed.unshift(char);
  }
  return reversed.join("");
}
const reversedString = reverseString("Hello");
console.log(reversedString); // "olleH"
Copy

21. Using the Array.from() method with a reverse loop:

Convert the string to an array, then loop in reverse and join characters.

function reverseString(str) {
  const arr = Array.from(str);
  let reversed = "";
  for (let i = arr.length - 1; i >= 0; i--) {
    reversed += arr[i];
  }
  return reversed;
}
const reversedString = reverseString("Hello");
console.log(reversedString); // "olleH"
Copy

22. Using Array.prototype.reduce() with substring():

Use reduce() to build the reversed string using substring().

function reverseString(str) {
  return str.split("").reduce((reversed, _, index) => str.substring(index, index + 1) + reversed, "");
}
const reversedString = reverseString("Hello");
console.log(reversedString); // "olleH"
Copy

23. Using the spread operator with reduce() and substring():

Use the spread operator to reverse the string using reduce() and substring().

function reverseString(str) {
  return [...str].reduce((reversed, _, index) => str.substring(index, index + 1) + reversed, "");
}
const reversedString = reverseString("Hello");
console.log(reversedString); // "olleH"
Copy

These are 23 different ways to reverse a string in JavaScript, each with its own implementation and approach. You can choose the one that suits your needs and coding style.

I hope this post has helped you to achieve your goals, Keep on coding.