How to Reverse a String in Javascript

in , , by Wemi Ibidun, September 20, 2021

Today's 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 to 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!!! ❤️


SHARE 0 comments

Add your comment

© Wemi's Coding Board · THEME BY WATDESIGNEXPRESS