Reverse a string using JavaScript?

nick asked this question 5 months ago
nick on Feb 18

How can I reverse a string in JavaScript?

For example, a given string of example should be converted into elpmaxe as the end result.

Is there a function or method that does this easily?

5 suggested answers
looper003 on Mar 14

This will reverse your string:


Code breakdown:

  • split(""): converts the string into an array containing each letter in the word. i.e. ["e", "x", "a", "m", "p", "l", "e"].
  • reverse(): reverses the order of the array.
  • `join(""): converts the array of letters back into a string format.
0 replies
coderguy on Mar 14

Here's how you could do it with a decrementing for loop:

let ogString = "example";

let newString = "";

for (var i = ogString.length - 1; i >= 0; i--) { 
    newString += ogString[i];

// newString = "elpmaxe"
0 replies
rusty1_rusty1 on Mar 14
function reverseString(string){
    return [...string].reverse().join("");
0 replies
moon_man41 on Mar 14

Using the reverse() method:

string.split("").reduce((rev, char) => char + rev, "");
0 replies
softwareguy411 on Mar 14

Loop through each character in the string:

function reverse(string) {
    let reversed = "";

    for (let character of string) {
      reversed = character + reversed;

    return reversed;
0 replies