Sometimes, we want to reverse a string in JavaScript.
In this article, we’ll look at how to reverse a string in JavaScript.
How to reverse a string in JavaScript?
To reverse a string in JavaScript, we use the string split
and array reverse
and join
methods.
For instance, we write
const reversedStr = str.split("").reverse().join("");
to call str.split
to split the string into an array of characters.
Then we call reverse
to return a reversed version of the array.
And then we call join
to combine the characters in the reversed array and return the string.
Conclusion
To reverse a string in JavaScript, we use the string split
and array reverse
and join
methods.