How to Reverse a String in JavaScript

To reverse a string in JavaScript, you can follow these steps:

  1. Convert the string to an array of characters using the “split()” method.
  2. Reverse the array using the “reverse()” method.
  3. Join the reversed array back into a string using the “join()” method.

Example

const inputString = 'redrum';
const reversedString = inputString.split('').reverse().join('');

console.log(reversedString);

Output

murder

In this example, we first used the split(”) method to convert the inputString variable (‘redrum’) into an array of characters.

In the next step, we used the reverse() method to reverse the order of the array elements.

Finally, we used the join(”) to concatenate the reversed array elements into a string.

The resulting reversed string is ‘murder’.

Leave a Comment