Simple Steps to Reverse a String in JavaScript Explained

We are going to learn how to reverse a string in JavaScript. JavaScript strings do not have a direct reverse method. I will follow three steps using array methods to convert the letters of your string in a reverse order. The basic working of this whole approach is simple. Convert the string to an array, reverse that array, and then join it back to a string. This is the entire logic of converting the string in a reverse order.

JavaScript String Reversal basics

  Simple Steps to Reverse a String in JavaScript Explained  4   For reversing the string I am going to use three different steps. The first step is convert the string to array. I will use the split method.   Simple Steps to Reverse a String in JavaScript Explained  1   The second step is to reverse the array. I will use the reverse method.   Simple Steps to Reverse a String in JavaScript Explained  2   In the third step I will join back to a string. I will use the join method.   Simple Steps to Reverse a String in JavaScript Explained  3  

Three steps

Step 1: Convert the string to an array with split. Step 2: Reverse the array with reverse. Step 3: Join the array back to a string with join.   Simple Steps to Reverse a String in JavaScript Explained  18     Simple Steps to Reverse a String in JavaScript Explained  16     Simple Steps to Reverse a String in JavaScript Explained  14  
// Step 1
const arr = 'hello'.split(''); // ['h', 'e', 'l', 'l', 'o']

// Step 2
arr.reverse(); // ['o', 'l', 'l', 'e', 'h']

// Step 3
const reversed = arr.join(''); // 'olleh'
  Simple Steps to Reverse a String in JavaScript Explained  15  

How it works

If I say that I am having a word named Ram and I use this method, in the first step it will make an array. Inside this I will have ‘R’, then ‘A’, and then ‘M’ after using the split method.   Simple Steps to Reverse a String in JavaScript Explained  5   In the second step, when I use the reverse function on the variable which is holding this array, it will convert it in reverse order. That is first ‘M’, then ‘A’, and then ‘R’.   Simple Steps to Reverse a String in JavaScript Explained  6   By using the join method, it will again convert this [‘M’, ‘A’, ‘R’] as elements into the letters that is “MAR”. This is how the whole logic works.   Simple Steps to Reverse a String in JavaScript Explained  7   For formatting strings after you build results like this, see template literals.

JavaScript String Reversal example

  Simple Steps to Reverse a String in JavaScript Explained  8   Let’s take a real example and understand how it can be done.
let str = 'hello';

// split - divide the string into characters based on no space delimiter
let arr = str.split('');
console.log(arr); // ['h', 'e', 'l', 'l', 'o']

// reverse - reverse the array
arr.reverse();
console.log(arr); // ['o', 'l', 'l', 'e', 'h']

// join - join back to a string with no space
let reversed = arr.join('');
console.log(reversed); // 'olleh'
  Simple Steps to Reverse a String in JavaScript Explained  17   You can also chain these three methods in one line.   Simple Steps to Reverse a String in JavaScript Explained  9  
let str = 'hello';
let reversed = str.split('').reverse().join('');
console.log(reversed); // 'olleh'
  Simple Steps to Reverse a String in JavaScript Explained  10   Read More: logical ops

Why split(”) uses no space

  Simple Steps to Reverse a String in JavaScript Explained  11   When I pass an empty string to split like split(”), it says that we divide the string into individual letters based on no space. The characters in “Ram” are not having any space between them, so split(”) indicates that we will split on that basis and get [‘R’, ‘A’, ‘M’].   Simple Steps to Reverse a String in JavaScript Explained  12   If I do this for “hello”, I get five different elements: ‘h’, ‘e’, ‘l’, ‘l’, ‘o’. After reverse, I get [‘o’, ‘l’, ‘l’, ‘e’, ‘h’], and after join with no space, I get “olleh”.   Simple Steps to Reverse a String in JavaScript Explained  13  

Final thoughts

JavaScript strings do not have a reverse method, so the three-step approach using split, reverse, and join is the way to reverse a string. Convert the string to an array of characters, reverse that array, and join it back to get the reversed string. This pattern is simple, readable, and works consistently for basic Latin characters. For converting text-based data into JavaScript values, see JSON.parse.

Leave a Comment