How to Reverse a String in Javascript

Today's programming tip is about how to reverse a string in Javascript, using only one line of code.





Explanation
If you are given a string to reverse. As an example, let's say you were asked to reverse my name 😜, Wemimo. This can be broken down into 4 steps ðŸ‘‡


const toReverse = 'Wemimo';

const toReverse = str => str.split('').reverse().join('');

Example: 

console.log(toReverse('Wemimo'));

//Expected output: omimeW


Step One
- Split the given string into an array of substrings (a substring is a character between a string)

stepOne = toReverse.split('');

Step Two
- Reverse the array of substrings

stepTwo = stepOne.split('');


Step Three
- Join the substrings together to have a single string

stepThree = stepOne.reverse();


Step Four
- Join the substrings together to have a single string

stepThree = stepTwo.join('');




With these 4 steps, you will be able to reverse any given string. That will be all for me today. Remember to stay positive ðŸ’¡ &  ❤️


Food for Thought: “Whether you want to uncover the secrets of the universe, or you just want to pursue a career in the 21st century, basic computer programming is an essential skill to learn.”- Stephen Hawking


HAPPY CODING!!! ❤️