How To Reverse A String In JavaScript
Maybe your like
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!
In today's lesson, we're going to learn how to reverse a string! Remember in my Web Basics Series, we covered these 3 methods. Let's now 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.
Split our string into an array of letters.
Download HD Image # 2. reverse()
Excellent, now that we have an array of letters. We can call our built-in array method to reverse the order.
Reverse the order of the items in our array.
Download HD Image # 3. join()
Now that our array contains the reversed letters. Let's convert the array back into a string.
Join the items in our array back into a string.
Download HD Image # 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 🥳
Download HD Image # More Solutions
Using reverse
function reverseString(str) { return str .split('') .reverse() .join(''); }Using reduce
function reverseString(str) { return [...str].reduce((accumulator, current) => { return current + accumulator; }); // OR One-Liner // return [...str].reduce((accumulator, current) => current + accumulator) }Using reduceRight
function reverseString(str) { return [...str].reduceRight((accumulator, current) => accumulator + current); }Using for loop
function reverseString(str) { let result = ''; for (let i = str.length - 1; i >= 0; i--) { result += str[i]; } return result; }Using sort
function reverseString(str) { return str .split('') .sort(() => 1) .join(''); }Using recursion
function reverseString(str = '') { const [head = '', ...tail] = str; if (tail.length) { return reverseString(tail) + head; } return head; }Know other solutions? Submit it here
Tag » How To Reverse A String Javascript
-
Three Ways To Reverse A String In JavaScript - FreeCodeCamp
-
How Do You Reverse A String In-place In JavaScript? - Stack Overflow
-
JavaScript Program To Reverse A String - Programiz
-
Reverse A String In JavaScript - GeeksforGeeks
-
How To Reverse A String In JavaScript - Stack Abuse
-
Three Ways To Reverse A String In JavaScript - Medium
-
Methods To Reverse String In JavaScript - Flexiple
-
How Do You Reverse A String In Place In JavaScript? - Tutorialspoint
-
verse() - JavaScript - MDN Web Docs
-
How Do I Reverse A String In JavaScript? - ReqBin
-
Reversing A String Using JavaScript
-
How To Reverse A String In Python - W3Schools
-
Reverse A String In JavaScript - DEV Community
-
How To Reverse A String In JavaScript - Mastering JS