Reverse a String in JavaScript (original) (raw)

Last Updated : 10 Jan, 2025

We have given an input string and the task is to reverse the input string in JavaScript.

reverse-a-string-in-js

Reverse a String in JavaScript

**Using split(), reverse() and join() Methods

The split() method divides the string into an array of characters, reverse() reverses the array, and join() combines the reversed characters into a new string, effectively reversing the original string.

JavaScript `

let s = "GeeksforGeeks"; const ans = s.split('').reverse().join(''); console.log(ans);

`

**Using Spread Operator

The spread operator(...) is used to spread the characters of the string str into individual elements. The reverse() method is then applied to reverse the order of the elements, and join() is used to combine the reversed elements back into a string.

JavaScript `

let s = "GeeksforGeeks"; const ans = [...s].reverse().join(""); console.log(ans);

`

Writing Your Own Method

Please refer reverse an string tutorial to see different algorithms and implementations of reversing an array.

Similar Reads