How to reverse a string in JavaScript

Reversing a string means changing the order of characters in a string so that the last character becomes the first, the second-to-last becomes the second, and so on. Essentially, the process involves flipping the string from end to start.

JavaScript doesn’t have a built-in method specifically for reversing strings like some other languages do. However, JavaScript does provide a reverse() method, but it is designed for arrays, not strings.

The reason for this is that strings in JavaScript are immutable, meaning their values cannot be changed after creation. The absence of a dedicated reverse method for strings is due to this immutability.

You can achieve string reversal using following techniques:

Using array methods

One of the simplest ways is to use the built-in methods of the String and Array objects. You take a string, splits it into a list of characters, reverses the list, and then joins the characters back into a string to form the reversed version of the original string.

const str = "Hello, world!";
const reversed_str = str.split("").reverse().join("");
console.log(reversed_str); // "!dlrow ,olleH"

Instead of using split() method, you can transform the string into an array of characters using the spread operator. Then reverse() and join() like above:

const str = "Hello, world!";
const reversed_str = [...str].reverse().join("");
console.log(reversed_str); // "!dlrow ,olleH"

In general, using array methods tends to be a solid and widely accepted practice for reversing strings in JavaScript.

Using a for loop

The algorithm for reversing a string using a for loop involves iterating through the characters of the original string in reverse order and building a new string by appending each character.

const str = "Hello, world!";
let reversed_str = "";
for (let i = str.length - 1; i >= 0; i--) {
  reversed_str += str[i];
}
console.log(reversed_str); // "!dlrow ,olleH"

Using recursion

It works by breaking down the problem into smaller subproblems (reversing substrings) and then combining the solutions to those subproblems to form the final reversed string.

First you call the reverse function recursively with a substring of the original string, starting from the second character to the end. Then you take the first character of the original string and appends it to the end of the reversed substring returned by the recursive call.

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

const str = "Hello, world!";
const reversed_str = reverse(str);
console.log(reversed_str); // "!dlrow ,olleH"

Using a string library

Voca (3.6k ⭐) is a JavaScript library designed for string manipulation. It provides a set of utility functions to simplify common string operations. It does include a method for reversing strings called reverse().

import v from 'voca'

let str = "Hello, world!";
let reversed_str = v.reverse(str);
console.log(reversed_str); // "!dlrow ,olleH"