How to reverse a string in Javascript?

Introduction

One of the most typical JavaScript interview questions during the technical round is how to reverse a string. Interviewers could ask you to build various techniques for reversing strings, to do it without the use of built-in methods, or even to do so by utilising recursion.

Several tens of methods might be used, eliminating the built-in reverse function as JavaScript lacks one.

Overview

A string is a grouping of letters, words, or other symbols. Reversing a string involves flipping all the characters so that the initial character becomes the last character and vice versa. Reversing a string can be used, among other things, to determine whether or not it is a palindrome.

Reverse a String With Built-In Functions

Three techniques will be used to solve this problem: String.prototype.split(), Array.prototype.reverse(), and Array.prototype.join().

  • The split() function divides a String object into an array of strings by dividing a string into substrings.
  • An array is placed in reverse using the reverse() function. The first array element changes into the final one, and vice versa.
  • An array's components are combined into a string using the join() function.

Code

// program to reverse a string

function reverseString(str) {

// return a new array of strings

const arrayStrings = str.split("");

// reverse the new created array elements

const reverseArray = arrayStrings.reverse();

// join all elements of the array into a string

const joinArray = reverseArray.join("");

// return the reversed string

return joinArray;

}

// take input from the user

const string = prompt('Enter a string: ');

const result = reverseString(string);

console.log(result);

write your code here: Coding Playground

Output

Enter a string: BoardInfinity

ytinifnIdraoB

Reverse a String With a Decrementing For Loop

We may traverse through each character in the string using for a loop. We may constantly add characters to a new string starting from the end of the string to the beginning of the string to create a reversed string:

Code

function reverseString(str) {

var newString = "";

for (var i = str.length - 1; i >= 0; i--) {

newString += str[i];

}

return newString;

}

reverseString(‘boardInfinity’);

write your code here: Coding Playground

Output

ytinifnIdraob

Using Recursion to Reverse the String

Recursion is a process in which a function calls itself until a condition is met. The substr() method returns the characters in a string beginning at the specified location, and The charAt() method returns the specified character from a string.

Code

function reverseString(str) {

if (str === "") return "";

else return reverseString(str.substring(1)) + str.charAt(0);

}

var string1 = "BoardInfinity";

console.log(reverseString(string1));

write your code here: Coding Playground

Output

ytinifnIdraoB

Conclusion

Recursion, loops, and other techniques can reverse a string.

  • We can revise a string by using the split() function, which creates an array of substrings of the text, the reverse() method, which reverses the order of the array; and the join() method, which creates an array from the elements of the array.
  • The string can easily be reversed using a decremented loop.
  • By using the procedure recursively and utilising the.substr() and.charAt() methods, a string may be reversed.