Blog

EN

How to reverse a string in JavaScript

2 min read

In JavaScript, there are several ways to reverse a string. The most common are as follows:

Array Methods

This is the simplest and most commonly used method.

const str = "hello";
const reversed = str.split("").reverse().join("");
console.log(reversed); // "olleh"
  • The split(“”) converts the string into an array of characters, something like this: [“h”, “e”, “l”, ‘l’, “o”].
  • With reverse(), you reverse the array.
  • And with join(“”), it joins this array to a string olleh.

With a `for`

const str = "hello";
let reversed = "";

for (let i = str.length - 1; i >= 0; i--) {
  reversed += str[i];
}

console.log(reversed); // "olleh"

With spread operator

const str = "hello";
const reversed = [...str].reverse().join("");
console.log(reversed); // "olleh"

For most cases, the first method using split will suffice; it is short and readable.

Share this article on

Avatar byandrev

Andres Parra

Software Engineer

I'm Andres Parra, Software Engineer passionate about developing scalable and innovative technological solutions. I specialize in building modern web applications, mastering a versatile stack that includes JavaScript, TypeScript, Python, and Java, along with frameworks like React, Next.js, and Spring Boot. I'm also interested in the latest technologies and tools for development.