JavaScript Program to Reverse a String

Updated on November 19, 2024
Reverse a string header image

Introduction

Reversing a string is a common operation in programming. In JavaScript, this can be accomplished in several different ways that suit various scenarios and coding styles. Such techniques are not only fundamental in many coding problems but also serve as a great way to understand string manipulation and JavaScript array methods.

In this article, you will learn how to reverse a string in JavaScript using a few different methods. Each section explains a particular approach, showing its implementation step-by-step through examples.

Using the Built-in Methods

Reverse Using Split, Reverse, and Join

  1. Convert the string into an array using the split('') method.

  2. Reverse the array using the reverse() method.

  3. Join the array back into a string using the join('') method.

    javascript
    function reverseString(str) {
        return str.split('').reverse().join('');
    }
    
    const originalString = "JavaScript";
    const reversedString = reverseString(originalString);
    console.log(reversedString);
    

    This example takes the string "JavaScript", splits it into an array of characters, reverses the array, and then joins the characters back into a string. The output will be "tpircSavaJ".

Using a Decrementing For Loop

Manual Reverse with For Loop

  1. Create an empty string for the reversed version.

  2. Loop through the original string from end to beginning.

  3. Append each character to the new string.

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

    In this script, the function loops backward through the string "Hello", constructing a new string from the last character to the first. The result is "olleH".

Using Recursion

Recursive Method to Reverse a String

  1. Check if the string is empty; if so, return an empty string.

  2. Else, return the last character plus a recursive call minus the last character.

    javascript
    function reverseStringRecursive(str) {
        if (str === "") {
            return "";
        } else {
            return str[str.length - 1] + reverseStringRecursive(str.substring(0, str.length - 1));
        }
    }
    
    const originalString = "Recursion";
    const reversedString = reverseStringRecursive(originalString);
    console.log(reversedString);
    

    This code uses recursion to build the reverse string by decomposing the input string until it’s empty. Each recursive call handles one character less than the previous call and adds the last character of the current string to the result. It ultimately constructs "noisruceR".

Conclusion

Reversing a string in JavaScript can be achieved using multiple techniques. From utilizing built-in methods like split(), reverse(), and join() for a clean and quick solution, employing a decrementing for loop for more control over the process, to leveraging recursion for an elegant, though less performance-efficient method. Experiment with these approaches to enhance your understanding and adapt your solutions to fit different contexts effectively. By trying out these examples, you ensure that you can handle basic string manipulations with ease in your future projects.