Reversing a String is indeed one of the most common and needed operations in JavaScript. During the journey of any Software Developer's career, a very important and basic question for interviews is "How to Reverse a String in JavaScript" There are a few ways to reverse a string in JavaScript. We can use loops, built-in functions, recursion and even regular expressions to solve the problem. In this post, I am going to show these approaches with examples. So, let's get started!! 🔶built-in Methods: split().reverse().join()The very first way that I want to discuss is perhaps the most commonly used way, which is using the built-in methods. First of all, we need to split the string to an Array of Single Characters i.e.("s","t","r","i","n","g"), then reverse the characters and lastly join them again to create the reversed string. In this example, basically three built-in methods are used. They are : 🔸
🔸 After the string is separated as an array of string, the 0🔸 The 2Chaining these MethodsWell, these three methods can be chained to make our code compact and clean in the following way and the output will be the same. However,instead of using 4 also, which is a ES6 syntax. It works the same way as before.So, the spread operator is exactly doing the same work as split() does, it is splitting the string object into single characters. 🔶Using Decrementing For LoopThis is an easy and oldest approach of reversing a string in JavaScript but works pretty well everywhere. 🔸 At first, newString created an empty string to host the new created string. We can also use the 5 syntax introduced by JavaScript ES6 which is also very easy to use and decreases the chances of mistake while writing the conditions of the loop.Much cleaner than the previous for loop, isn't it?? But works really fine! 🔸 The "c" in the for loop condition is taking each of the letter of the string as a single character. For better understanding I have added two console.log() statement in the following picture and also you can see the output in the terminal. 🔶The Recursive Approach: Recursion MethodUsing the Recursion method is another very well-known approach to reverse a string in JavaScript. We need two methods to perform this recursive approach. One is the 6 method and another is the 7 method.Let's see an example: 🔸 String.prototype.substr() method returns a portion of the string, beginning at the specified index and extending for a given number of characters afterwards. 8 returns "ecursion".🔸 7 method returns the specified character from a string.In our example, the part 0 returns "R".🔸 1 firstly returns the portion of the string , starting at the index of the first character to include in the returned substring . Second part of the method hits the if condition and the most highly nested call returns immediately.We need to remember that this method won’t have just one call but have several nested calls. Nevertheless, this approach is not a best approach for reversing a string as the depth of the recursion is equal to the length of the string and in case of a very long string, it takes much more time than any other method and the size of the stack is a major concern here. 🔶 Using Array.prototype.reduce() MethodThe 2 method executes a reducer callback function on each element of the array, passing in the return value from the calculation on the preceding element and returns a single value as the final result. The syntax can be written as: 3Let's see an example of this.
Enter fullscreen mode Exit fullscreen mode 🔸 The reverseString function takes a string str as parameter. This code block can be more compact if we use JavaScript ES6 syntax. 👇
Enter fullscreen mode Exit fullscreen mode Making it a one-line code:
Enter fullscreen mode Exit fullscreen mode 🔶Using Regular Expressions (RegEx)This is one of the rarest and trickiest approaches of reversing a string but developers who love to play with JavaScript Regular Expressions can definitely try this approach. Also, in any interview, if you can show this, that might be a plus point as it is such an approach that people generally don't use and they will know that you have mastered another skill that is Regular Expressions!
Enter fullscreen mode Exit fullscreen mode 🔸The simplest quantifier in RegEx is a number in curly braces: {n}. A quantifier is appended to a character (or a character class, or a [...] set etc) and specifies how many we need. 4🔸 The RegEx here is mainly doing the work of separating the string object into single characters of an array. After separating the reverse() and join() method is working exactly like it worked with 5 or 6 as I have shown above in the built-in approach. |