How do I reverse a string in JavaScript?

JavaScript does not have a built-in method for reversing a string. To reverse a string in JavaScript, you can use a combination of three built-in methods: split(), reverse(), and join(). The first method is used to split a string into an array of characters and returns a new array. The second method is used to reverse the order of elements in an array (the first element becomes last, and the element becomes first). The third method is used to concatenate all elements of an array into a string and return a new string. Another way to reverse a string in JavaScript is to use a combination of the substr() and charAt() methods. The first method returns a substring of the string, and the second method returns the specified character of the string. You can also reverse a string with a for loop, using a decrement (or increment) loop to loop through per character of the string and create a new reversed string. In this JavaScript Reverse String example, we reverse a string using the split(), reverse(), and join() methods. Click "Run" to run the JavaScript string reversal example online and see the result.
How do I reverse a string in JavaScript? Execute
let str = 'JavaScript Reverse String Example';
let reversed = str.split('').reverse().join('');

console.log(reversed);
Updated: Viewed: 885 times

How to reverse a string using built-in JavaScript methods?

You can reverse a string in JavaScript by calling the split(), reverse(), and join() built-in methods in sequence. Below is an example reverse string in JavaScript:

JavaScript Reverse String Example
let str = 'JavaScript';
    
let splitString = str.split('')
let reverseArray = splitString.reverse();
let newString = reverseArray.join('');

console.log(newString);

// output: tpircSavaJ

How to reverse a string with a for loop in JavaScript?

The following is an example of reversing a JavaScript string using a for loop:

JavaScript Reverse String with for loop Example
let str = 'JavaScript Reverse String Example';

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

console.log(newString)

// output: elpmaxE gnirtS esreveR tpircSavaJ

How to reverse a string using recursion in JavaScript?

You can reverse a string using recursion in JavaScript. The recursion includes two JavaScript methods: substr() and charAt(). The substr() returns a substring of the string, while the charAt() returns the specified character of the string.

JavaScript Reverse String using Recursion Example
function stringReverse(string) {
  if (string === "")
    return "";
  else
    return stringReverse(string.substr(1)) + string.charAt(0);
}

console.log(stringReverse("Reverse String"));

// output: gnirtS esreveR

You can also recurse using the conditional (ternary) operator ("?"):

JavaScript Reverse String with Conditional Operator Example
function stringReverse(string) {
  return (string === '') ? '' : stringReverse(string.substr(1)) + string.charAt(0);
}

console.log(stringReverse("Reverse String"));

// output: gnirtS esreveR

See also