Reverse a String in JavaScript

Last Updated : 20 Dec, 2025

We have given an input string, and the task is to reverse the input string in JavaScript.

reverseAStrinJS
Reverse a String in JavaScript

Below are the following approaches by which we can reverse a string in JavaScript:

1. Using split(), reverse() and join()

In this approach, we’ll use three built-in methods: split(), reverse(), and join().

  • split(): This method splits a string into an array of substrings based on a specified delimiter.
  • reverse(): This method reverses the order of elements in the array, swapping the first element with the last, and so on.
  • join(): This method combines all elements of the array back into a single string, using an optional separator.
JavaScript
let str = "hello";
let reversedStr = str.split('').reverse().join('');
console.log(reversedStr); 

Output
olleh

2. Using a for loop

We use a for loop to iterate through the string from the last character to the first. At each step, we build a new string by adding the current character to the result.

JavaScript
let name = "GeeksforGeeks";
let reverseString = '';
for (let i = name.length - 1; i >= 0; i--) {
    reverseString += name[i];
}
console.log(reverseString); 

Output
skeeGrofskeeG

3. Using Recursion

For this solution, we will use two methods: the String.prototype.substr() method and the String.prototype.charAt() method.

The substr() method returns the portion of a string starting at the specified position and continuing for a given number of characters.

Example:

"hello".substr(1); // "ello"

The charAt() method returns the character at the specified index in a string.

Example:

"hello".charAt(0); // "h"

Note: This approach is not optimal for very long strings, as deep recursion can cause performance issues and stack overflow concerns.

JavaScript
function reverseString(str) {
    if (str === "") {
        return str;
    } else {
        return reverseString(str.substr(1)) + str[0];
    }
}
console.log(reverseString("GeeksforGeeks")); 

Output
skeeGrofskeeG

4. Using Spread Operator

The spread operator(...) is used to spread the characters of the string str into individual elements. The reverse() method is then applied to reverse the order of the elements, and join() is used to combine the reversed elements back into a string. 

JavaScript
let s = "GeeksforGeeks";
const ans = [...s].reverse().join("");
console.log(ans);

Output
skeeGrofskeeG


Comment

Explore