Typefully

Ways to Reverse a String in Javascript

Avatar

Share

 • 

3 years ago

 • 

View on X

3 Ways to Reverse a String(Word) in JavaScript šŸš€ Thread 🧵
Input : String //vipin Output : Reverse of the given String //nipiv
1. Using loop • Create a reversed variable to store the reverse string. • Starting at the last character of the given string passed to the function. • During each iteration reversed gets concatenated with itself and the current character after loop end return reversedStr.
2. Using Methods • Here, split() method splits a String into an array of string by separating the string into sub strings. • After, reverse() method reverses an array in place. • Last, join() method joins all elements of an array into a string and return it.
3. Using Recursion • check If the string is empty, return ā€˜ ’ which is an empty string. • If the string is not empty, append the first character at last and make a recursive function call sub-string starting from the first character to the end of the string.
• Return the returned value from the recursive call of sub-string by appending first character of string at last.
End thread 🧵 If this thread helpful for you then 1. Do Retweet the FIRST TWEET. 2. Follow me and turn on the notifications: @personalvipin Thankyou ā£ļø
Avatar

Vipin

@SocialWithAI

HTML is šŸ”„ | CSS is ā£ļø Hi šŸ‘‹ I'm Vipin, I'm learning web2 and sharing my knowledge with you guys in easy ways.