Reverse a String in JavaScript
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"
Method 1:
- Check the input string if a given string is empty or just has one character or it is not of string type then it returns “Not Valid string”.
- If the above condition is false create an array where we can store the result. Here revArray[] is the new array.
- Loop through the array from the end to the beginning and push each and every item in the array revArray[].
- Use the join() prebuilt function in JavaScript to join the elements of an array into a string.
Example: This example shows the above-explained approach.
javascript
<script> function ReverseString(str) { // Check input if (!str || str.length < 2 || typeof str!== 'string' ) { return 'Not valid' ; } // Take empty array revArray const revArray = []; const length = str.length - 1; // Looping from the end for (let i = length; i >= 0; i--) { revArray.push(str[i]); } // Joining the array elements return revArray.join( '' ); } console.log(ReverseString( "Geeks for Geeks" )) </script> |
Output:
skeeG rof skeeG
Method 2:
- Use split() inbuilt function in JavaScript to split a string into an array of characters i.e. [ ‘G’, ‘e’, ‘e’, ‘k’, ‘s’, ‘ ‘, ‘f’, ‘o’, ‘r’, ‘ ‘, ‘G’, ‘e’, ‘e’, ‘k’, ‘s’ ]
- Use the reverse() function in JavaScript to reverse the array of characters i.e. [ ‘s’, ‘k’, ‘e’, ‘e’, ‘G’, ‘ ‘, ‘r’, ‘o’, ‘f’, ‘ ‘, ‘s’, ‘k’, ‘e’, ‘e’, ‘G’ ]
- Use join() function in JavaScript to join the elements of an array into a string.
Example: This example shows the above-explained approach.
javascript
<script> // Function to reverse string function ReverseString(str) { return str.split( '' ).reverse().join( '' ) } // Function call console.log(ReverseString( "Geeks for Geeks" )) </script> |
Output:
skeeG rof skeeG
Method 3:
- Use the spread operator instead of a split() function to convert the string into an array of characters i.e. [ ‘G’, ‘e’, ‘e’, ‘k’, ‘s’, ‘ ‘, ‘f’, ‘o’, ‘r’, ‘ ‘, ‘G’, ‘e’, ‘e’, ‘k’, ‘s’ ]. Learn more about the spread operator here Spread Operator
- Use the reverse() function in JavaScript to reverse the array of characters i.e. [ ‘s’, ‘k’, ‘e’, ‘e’, ‘G’, ‘ ‘, ‘r’, ‘o’, ‘f’, ‘ ‘, ‘s’, ‘k’, ‘e’, ‘e’, ‘G’ ]
- Use the join() function in JavaScript to join the elements of an array into a string.
Example: This example shows the above-explained approach.
javascript
<script> const ReverseString = str => [...str].reverse().join( '' ); console.log(ReverseString( "Geeks for Geeks" )) </script> |
Output:
skeeG rof skeeG
Method 4:
- Use the spread operator to convert a string into an array of characters.
- 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
<script> // Function to reverse the string function ReverseString(str) { // Returning reverse string return [...str].reduce((x, y) => y.concat(x)); } console.log(ReverseString( "Geeks for Geeks" )) </script> |
Output:
skeeG rof skeeG
Please Login to comment...