Loop through a string in reverse order in JavaScript
This post will discuss how to loop through a string in reverse order in JavaScript.
Looping through a string in reverse order means looping through each character in the string from the end to the beginning and performing some action or operation on it. Here are some of the most common functions:
1. Using a for loop
One way is to use a for loop with the length property and the charAt() function of the string object. This function will loop through the string from the last character to the first one, using an index variable to access each character. For example, we can use the following code to loop through the string "Hello" in reverse order and print each character:
|
1 2 3 4 5 |
let str = "Hello"; for (let i = str.length - 1; i >= 0; i--) { console.log(str.charAt(i)); // print each character } |
Output:
o
l
l
e
H
We can also use a while loop in place of for loop, using a loop counter variable to access each character in the string by its index, starting from the last index and decrementing by 1 until reaching -1. For example:
|
1 2 3 4 5 6 7 8 9 10 |
let str = "Hello"; // The initial value of the counter let i = str.length - 1; // Loop while the counter is not negative while (i >= 0) { console.log(str[i]); // Print the character at index i i--; // Decrement the counter } |
Output:
o
l
l
e
H
2. Using split() and reverse() function
Another way is to use the split() function and the reverse() function of the array object. This function will first split the string into an array of single-character strings, using an empty string as the separator. Then it will reverse the order of the array elements, using the reverse() function. Finally, it will loop through the reversed array elements, using a for loop or any other iteration function. For example, using the same string as before, we can iterate over its characters backward using split() and reverse() like this:
|
1 2 3 4 5 6 7 8 9 10 11 |
let str = "Hello"; // split the string into an array of characters let arr = str.split(""); // reverse the order of the array elements arr.reverse(); for (let i of arr) { console.log(i); // print each character } |
Output:
o
l
l
e
H
3. Using a recursive function
A third way is to use a recursive function, which is a function that calls itself until a base case is reached. This function will take a string as an argument and print its last character. Then it will call itself with a substring that excludes the last character, until the string is empty. For example, we can iterate over the characters of a string backwards using a recursive function like this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
function printReverse(str) { // base case: empty string if (str == "") { return; // stop recursion } // print last character console.log(str.charAt(str.length - 1)); // call itself with substring that excludes last character printReverse(str.slice(0, -1)); } let str = "Hello"; // call the function with original string printReverse(str); |
Output:
o
l
l
e
H
That’s all about looping through a string in reverse order in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)