How to use for Loop In Javascript

The for loop is used to iterate through the characters of the string in reverse order. Starting from the last character (str.length – 1) and character pushed to the new reverse string one by one.

Example: The below code implements the for loop to reverse a string.

Javascript
// Function to reverse string
function reverseString(str) {
    let strRev = "";
    for (let i = str.length - 1; i >= 0; i--) {
        strRev += str[i];
    }
    console.log(strRev);
}

// Function call
reverseString("w3wiki");
reverseString("JavaScript");
reverseString("TypeScript");

Output
skeeGrofskeeG
tpircSavaJ
tpircSepyT

Reverse a String in JavaScript

We have given an input string and the task is to reverse the input string in JavaScript. It is a very common question asked in a JavaScript interview. There are various methods to reverse a string in JavaScript, which are described below with examples.

Reverse a String in JavaScript

Examples:

Input: str = "w3wiki"
Output: "skeeGrofskeeG"

Input: str = "Hello"
Output: "olleH"

Similar Reads

Reverse a String in JavaScript

There are the common approaches to reverse a string in JavaScript. These are:...

1. Using reduce() and split() Methods

The split() method divides the string into an array of characters, and reduce() combines the characters in reverse order using the accumulator, effectively reversing the original string....

2. Using split(), reverse() and join() Methods

The split() method divides the string into an array of characters, reverse() reverses the array, and join() combines the reversed characters into a new string, effectively reversing the original string....

3. 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....

4. Using Array.form() and reverse() Methods

The Array.from() is used to convert the string into an array of individual characters. The reverse() method is then applied to reverse the order of the elements in the array. Finally, the join() is used to combine the reversed elements back into a string....

5. Using Spread Operator and reduce() method

The spread operator can be used to convert a string into an array of characters and use reduce() function in JavaScript to make a reverse string from an array by concatenating the string in the forward direction....

6. Using for Loop

The for loop is used to iterate through the characters of the string in reverse order. Starting from the last character (str.length – 1) and character pushed to the new reverse string one by one....

7. Using substring() and a Decrementing Index

The substring() method is used to extract the character at index i and append it to the reversed string. The index i is then decremented....

8. Using Recursion

In a recursive approach a function repeatedly calls itself, taking the substring from the second character and concatenating it with the first character, until the base case is reached, reversing the string....

9. Using Stack

In this approach we use stack data structure. We iterate through each character of the input string and push it onto the stack. Then, we pop each character from the stack one by one and concatenate them to form the reversed string....