In this article, we are given an input string and the task is to reverse the input string. We have various methods to reverse a string in JavaScript, some of them are described below with examples.
Examples:
Input: str = "Geeks for Geeks"
Output: "skeeG rof skeeG"
Input: str = "Hello"
Output: "olleH"
here are some common approaches to reverse a string in javascript:
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.
Example: This example shows the above-explained approach.
Javascript
const str = "Geeks for Geeks" ;
const reversedString = str.split( "" ).reduce((acc, char) => char + acc, "" );
console.log(reversedString);
|
In this approach, 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.
Example: This example shows the above-explained approach.
javascript
function ReverseString(str) {
return str.split( '' ).reverse().join( '' )
}
console.log(ReverseString( "Geeks for Geeks" ))
|
Output:
skeeG rof skeeG
In this approach, 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.
Example: This example shows the above-explained approach.
javascript
const str = "Geeks for Geeks" ;
const reversedStr = [...str].reverse().join( "" );
console.log(reversedStr);
|
In this approach, Array.from() is used to convert the string str into an array of individual characters. The reverse() method is then applied to reverse the order of the elements in the array. Finally, join() is used to combine the reversed elements back into a string.
Example: In this example, we are implementing the above approach.
Javascript
const str = "Geeks for Geeks" ;
const reversedStr = Array.from(str).reverse().join( "" );
console.log(reversedStr);
|
In this approach, we use the spread operator 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.
Example: This example shows the above-explained approach.
Javascript
function ReverseString(str) {
return [...str].reduce((x, y) => y.concat(x));
}
console.log(ReverseString( "Geeks for Geeks" ))
|
Approach 6 : Using for loop
In this approach, a for loop is used to iterate through the characters of the string in reverse order. Starting from the last character (str.length – 1), the loop decrements i until it reaches 0. During each iteration, the current character (str[i]) is appended to the reversed string.
Example: In this example, we are using the above-explained approach.
Javascript
const str = "Geeks for Geeks" ;
let reversedStr = "" ;
for (let i = str.length - 1; i >= 0; i--) {
reversedStr += str[i];
}
console.log(reversedStr);
|
Approach 7: Using substring() and a decrementing index
In this approach, 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.
Example: In this example, we are using the above-explained approach.
Javascript
const str = "Geeks for Geeks" ;
let reversedStr = "" ;
let i = str.length - 1;
while (i >= 0) {
reversedStr += str.substring(i, i + 1);
i--;
}
console.log(reversedStr);
|
Approach 8: Using recursion
In this approach, The recursive approach 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.
Example: In this example we are using above-explained approach.
Javascript
function reverseString(str) {
if (str === "" ) {
return "" ;
} else {
return reverseString(str.substr(1)) + str.charAt(0);
}
}
const reversed = reverseString( "Geeks for Geeks" );
console.log(reversed);
|