Professional Documents
Culture Documents
How to Reverse a
String in JavaScript
Welcome to the first Mini Code Tutorial!
@fototeka.inikas sent a DM to me saying I should
🤣 😂
start a shorter tutorial series. He said I would be
number 1 if I did it . So let’s see if he’s right .
JK. Doing this shorter tutorial have been on my list
for awhile. But have you every had a huge list of
things to do, and you never end up doing any of it
all. Anyways, I’m happy for the nudge!
👍
apply them and solve this popular algorithm
challenge
split()
reverse()
join()
Download HD Image
The Challenge
Write a function that reverse a string.
Download HD Image
1. split()
In JavaScript, there is no built-in method to reverse
a string. There is however, a built-in method to
reverse an array. So the first step is convert our
string into an array.
2. reverse()
Excellent, now that we have an array of letters. We
can call our built-in array method to reverse the
order.
3. join()
Now that our array contains the reversed letters.
Let's convert the array back into a string.
Final Solution
And there we have it! We can chain our methods
together to a nice function. Congratulation, you
🥳
have now learned how to reverse a string in
JavaScript
PICTORIALS
Step by Step Code Tutorials 👣
1 How to Reverse a String in
JavaScipt
Using reverse
js
function reverseString(str) {
return str
.split('')
.reverse()
.join('');
Using reduce
js
function reverseString(str) {
});
// OR One-Liner
Using reduceRight
js
function reverseString(str) {
js
function reverseString(str) {
result += str[i];
return result;
Using sort
js
function reverseString(str) {
return str
.split('')
.sort(() => 1)
.join('');
Using recursion
js
function reverseString(str = '') {
if (tail.length) {
return head;
MORE COURSES
Flexbox30
- Learn Flexbox with 30 Code Tidbits ✨
🎄
CodeTidbits30
- 30 days of the best JS, CSS, HTML
tidbits
Web Basics
- Web Basics Explained with Tidbits 🍎
TOP TIDBITS
Converting Object to Array in JavaScript How to Remove Array Duplicates in ES6 How to Deep Clone an Array in J