How to Reverse a String in JavaScript?


The process of reversing a string includes switching the order of all the characters/letters of the string so that the first letter becomes the last and vice versa. More specifically, knowing how to reverse a string can be useful in JavaScript programming when developers need to display a string in reverse order or handle the letters in a different way. There are various situations when reversing strings may be useful, such as encryption or obfuscation algorithms or a text editor or word processor’s search function.

This tutorial will demonstrate the methods for reversing a string in JavaScript.

How to Reverse a String in JavaScript?

To reverse a string in JavaScript, the following approaches can be used:

JavaScript Pre-built methods

String Iteration

Recursion

Method 1: Reverse a String in JavaScript Using “Pre-built” Methods

For reversing a string, use the list of JavaScript predefined methods including the “split()” method, the “reverse()” method, and the “join()” method. The “split()” method splits or divides the string into an array of characters based on any specific separator, and the “reverse()” method will reverse the whole array. If no separator is provided in the split() method, it will split the string into an array composed of distinct/ individual characters. Finally, all the array elements will join using the “join()” method.

Syntax

The given syntax is used for reversing strings using JavaScript Pre-built methods:

str.split('').reverse().join('')

Example

Here, we will define a function that will accept a string as a parameter. Call the JavaScript Pre-built methods for splitting, reversing, and again joining the string and display the reversed string on the console:

function reverseString(str) {
let reversedString = str.split('').reverse().join('');
console.log(reversedString);
}

Now, we will call the function two times by passing a string as an argument:

reverseString("welcome");
reverseString("madam");

As you can see, the output has successfully reversed the strings. The second string “madam” is a palindrome that will remain the same in reverse order:

Method 2: Reverse a String in JavaScript Using “String Iteration”

You can also reverse the string using iteration. To do so, iterate the string using a loop and construct a new string in reverse order. For this, you can use any loop including, for loop, while loop, and so on.

Example

In the following example, we will iterate the string using the “for” loop until the last character of the string or the length-1 and then, add them into an empty string from the last character:

function reverseString(str) {
let reversedString = '';
for (let i = str.length - 1; i >= 0; i--) {
reversedString += str[i];
}
console.log(reversedString); 
}

Call the function by passing a string as an argument:

reverseString("string");
reverseString("iteration");
reverseString("level");

Output

Method 3: Reverse a String in JavaScript Using “Recursion”

Another way to reverse a string is to use a “recursion()” function. It is a powerful technique for reversing JavaScript strings. In recursion, a recursive function is created that accepts a substring starting from the second character and recursively calls itself until the string becomes empty. Then, concatenate the first character of each recursive call to reverse the string.

Example

In the given example, we will define a function called a recursive function that will return an empty string if the string is empty. Otherwise, it calls itself by taking a substring starting from the second character as a parameter and finally concatenating the first character of each recursive call to reverse the string:

function reverseString(str) {
if (str === '') {
return '';
} else {
return reverseString(str.substr(1)) + str.charAt(0);
}
}

Call the recursive function by passing a string as an argument:

console.log(reverseString("hello"));
console.log(reverseString("racecar"));

Output

We have provided all the essential instructions related to the reversing string in JavaScript.

Conclusion

To reverse a string in JavaScript, use the JavaScript Pre-built methods, including the “split()” method, the “reverse()” method, and the “join()” method, string iteration, or the recursion technique. All of these techniques perform well and give a reversed string. You can select/choose anyone based on your needs. This tutorial demonstrated the methods for reversing a string.

Print Friendly, PDF & Email
Categories