How To Reverse A String In JavaScript
3 Ways To Reverse A String In JavaScript

I'm a full stack developer based in Australia. I write articles about Node.js, React, JavaScript & Web Development to make your learning journey easier ๐
Search for a command to run...
3 Ways To Reverse A String In JavaScript

I'm a full stack developer based in Australia. I write articles about Node.js, React, JavaScript & Web Development to make your learning journey easier ๐
No comments yet. Be the first to comment.
Introduction Website performance is an important aspect of web development. It consists of mainly two things: The speed of your website How usable is your website for visitors? Importance of website performance Website performance is very importa...

There are plenty of native web APIs that have a variety of uses. Here are 5 useful APIs that I use quite often in my web development projects. Clipboard API The clipboard API is a simple method to copy text to a user's clipboard. This can be used in ...

Hey All ๐ In this article, we'll be covering the Fetch API in JavaScript how you can use it to make HTTP requests. What is the Fetch API? The fetch() method provides a simple way to make HTTP requests in JavaScript. It is natively in the browser, me...

Hey All ๐ In this article, we'll be covering 4 mistakes commonly made by people while learning to code that will seriously impact their coding journey. #1 - Copying and Pasting The first mistake I'll talk about is copying and pasting. We've all don...

Introduction There are plenty of units in CSS. From px to vh, there is an abundance of units all with their distinctive use cases. Considering the amount of options, its perfectly reasonably to be confused as to which unit you should use and where. T...

In this short tutorial, we will look at 3 different ways of reversing a string in JavaScript.
Lets get right into it!
In the first method, we will reverse a string by looping through each character and adding the character to the start of a new string so it reverses itself as the loop goes on.
This looks like this:
const reverseAString = str => {
let reversedStr = ""
for(let i in str) {
const letter = str[i]
reversedStr = letter + reversedStr
}
return reversedStr
}
We are simply creating a function called reverseAString that loops through each letter of the passed in str and adds it to the start of the reversedStr variable it creates. Finally, it returns the reversedStr variable which stores the reversed string.
This method is the longest of the methods we will be covering so I wouldn't recommend it but it does work.
In the array method, we will split the string into an array, reverse the array and then join the array back into a string.
In code:
const reverseAString = str => {
return str.split("").reverse().join("")
}
This method is pretty self-explanatory, using the .split() method to split the string into an array, using the .reverse() method to reverse the array and then using the .join() method to join it back into a string.
This method is only one line of code (excluding creating the function) so I think its a great solution to reverse a string in JavaScript.
ES6 introduced various array methods that allow us to do cool stuff with arrays. One of which is the reduce method which essentially reduces an array down to a value.
We can use the .reduce() method to reverse a string:
const reverseAString = str => {
return str.split("").reduce((reversedStr, letter) => letter + reversedStr, '');
}
This method simply splits the string into an array using the .split() method and then reverses it using the .reduce() method. To learn more about the reduce method check out my JavaScript High Order Array Methods article.
We've covered 3 different methods of reversing a string in JavaScript in this article. I hope you've learned something useful.
๐ Thanks for reading this article!
If you like what I do and would love to see more related content, follow me on my other social platforms:
Github: Blake-K-Yeboah
LinkedIn: Blake Yeboah
You can also show your support by buying me a coffee ๐