How to reverse a string in Javascript – Fast tips

Valerio Barbera

In this article, we’ll look at three basic ways to reverse a string in JavaScript: utilizing the built-in reverse() method, a for loop, and the spread operator + reverse(). We’ll go over the advantages and disadvantages of each method and show you how to use them in your code.

Method 1: Using the reverse() function

The built-in array reverse() method is the simplest way to reverse a string in JavaScript. This method alters the original string by reversing the character order. Here’s an illustration:

let original = "Hello World";
let reversed = original.split('').reverse().join('');
console.log(reversed);

We begin by transforming the string into an array of characters using the split() method. Then we use the reverse() method to reverse the order of the characters in the array. Finally, the array is converted back into a string using join(). “dlroW olleH” is the resulting reversed string 😅.

Pros

  • It can be written in one line of code.
  • It alters the original array in place, thus no new variable is needed to store the reversed array

Cons

  • It alters the original string, which can be troublesome if other sections of your code rely on the original string.

Method 2: Using a for loop

let original = "Hello World";
let reversed = "";
for (let i = original.length - 1; i >= 0; i--) {
    reversed += original[i];
}
console.log(reversed);

In this example a for loop is used to go through the original string, beginning with the final character and finishing with the first. We add the current character to the reversed string on each iteration.

Pros

  • It is a versatile approach that can be used to add intermediate manipulations during the reverse process.
  • It does not change the original string, it can be used when the original string must be retained.

Cons

  • It necessitates the use of an additional variable (reversed) to record the reversed string, which can consume additional memory.
  • When compared to the reverse() function, it takes more code to be written.

Method 3: Using the Spread Operator and reverse()

This is the same concept of the “Method 1” using the Spread Operator instead of the split() method. Here’s an example:

let original = "Hello World";
let reversed = [...original].reverse().join('');
console.log(reversedString);

Pros

  • It is a very concise way that may be accomplished with a single line of code
  • it does not change the original string, it can be used when the original string must be retained

Cons

  • This approach is incompatible with Internet Explorer or older version of other browsers. So it may not be appropriate for browsers compatibility.

New to Inspector? Try it for free now

I hope this article can help you make some optimization to your application.

Are you responsible for application development in your company? Consider trying my product Inspector to find out bugs and bottlenecks in your code automatically. Before your customers stumble onto the problem.

Inspector is usable by any IT leader who doesn’t need anything complicated. If you want effective automation, deep insights, and the ability to forward alerts and notifications into your messaging environment try Inspector for free. Register your account.

Or learn more on the website: https://inspector.dev

Related Posts

Create a query for histogram charts with MySQL – Tutorial

To create a statistical query to build a histogram chart with MySQL, you can use the COUNT() function along with GROUP BY to count occurrences of values within a specified range or category created by the grouping constraint.  Especially for time series data there are a lot of use cases for histograms like monitoring the

Try the new Google Chat notification channel

Hi, I’m happy to announce that Google Chat is now available as a notification channel for your application. Now you can receive all important notifications of what happens in your application in your Google Chat environment in real time. Inspector Notification Channels allow you to create an automated and  informed workplace, helping your collaborators to

AI content creation to help you improve the Developer Experience

Co-founders are the first people in the trenches for any start-up, taking the idea and turning it into a sustainable business that can really make other people’s life better. In these early years of Inspector I have never told about my co-founders thanks to whom I was able to spend all my time building the