JS Example for Beginners: JavaScript Program to Reverse a String

(JavaScript programming Example for Beginners)

JavaScript Program to Reverse a String

In this tutorial, you will learn to write a JavaScript program that reverses a string.


Example 1: Reverse a String using for loop

// program to reverse a string

function reverseString(str) {

    // empty string
    let newString = "";
    for (let i = str.length - 1; i >= 0; i--) {
        newString += str[i];
    }
    return newString;
}

// take input from the user
let string = prompt('Enter a string: ');

let result = reverseString(string);
console.log(result);

Output

Enter a string: hello world
dlrow olleh

In the above program, the user is prompted to enter a string. That string is passed to the reverseString() function.

Inside the reverseString() function,

  • An empty newString variable is created.
  • The for loop is used to iterate over the strings. During the first iteration, str.length - 1 gives the last element. That element is added to the newString variable.
    This process continues for all the string elements.
  • The value of i decreases in each iteration and continues until it becomes 0.

Example 2: Reverse a String using built-in Methods

// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    let arrayStrings = str.split("");
   
    // reverse the new created array elements
    let reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    let joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
let string = prompt('Enter a string: ');

let result = reverseString(string);
console.log(result);

Output

Enter a string: hello
olleh

In the above program, the built-in methods are used to reverse a string.

  • First, the string is split into individual array elements using the split() method. str.split("") gives [“h”, “e”, “l”, “l”, “o”].
  • The string elements are reversed using the reverse() method. arrayStrings.reverse() gives [“o”, “l”, “l”, “e”, “h”].
  • The reversed string elements are joined into a single string using the join() method. reverseArray.join("") gives olleh.

 

JS Example for Beginners: JavaScript Program to Reverse a String

Sign up to get end-to-end “Learn By Coding” example.



Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.