Reverse String –
A reverse string is a concept for reverse a string and converts to a new string. There is no built-in method in String, which can achieve this concept.
We need to convert a string to an array and then reverse it. At last, we join that array and make a string.
Another way of doing it. you can use a loop to reverse a string. As you can also access string like an array using an index.
So, here we declare a variable string using let keyword and assign “racecar” to it.
Then, we use 3 array method to achieve a reverse split(), reverse() & join().
First, we convert a string to an array using the split() method. Then reverse it using the reverse() method and now convert array to string using join() method.
Now, we `console.log(string);` to find out, what is the output? or Does it really working? Who knows, you need to figure it out.
First, we declare variable string.
let string = “racecar”;
Here, we use a string split() method because with the help of it. We are able to convert string to an array.
The string does not have a reverse() method but array had a reverse() method to reverse an array element.
string = [ “r”, “a”, “c”, “e”, “c”, “a”, “r” ];
string = “racecar”
Point To Note: “racecar” still gives “racecar“. As the reversed version is the same as the original string.
And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page
If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.