DEV Community

Cover image for How to reverse a string in JavaScript
Usenmfon
Usenmfon

Posted on

How to reverse a string in JavaScript

String reversal is primarily concerned with changing the index position of a string in either ascending or descending pattern. i.e. going from come to emoc. In this short tutorial, we will be looking at various ways to handle this programming task.

Alt Text

Task: Reverse the given string: Hello
Note: This task could be solved in numerous ways, but we will take a look at two methods on how to go about this. There are;

  1. Imperative approach
  2. Declarative approach (A single line of code does the work)

Imperative approach

Pseudocode:
We will be defining a function (just to make our code organized at this point)

  1. Define a function and give it a name
  2. The function will accept a single parameter (which is the input to the function)
  3. Declare a variable and initialize it with the length of the function parameter
  4. Declare a variable which accepts an empty string which will be used to stored our new reversed string
  5. Iterate through the given string from the last index in the string array.
  6. Return the the concatenated reversed string
  7. Call the function with an argument

Solution:

function reverseThisString(inputString) {
    let stringLength = inputString.length
    let reversedString = '';
    for (let i = stringLength - 1; i >= 0; i--) {
        reversedString += inputString[i];
    }
    return reversedString;
}
console.log(reverseThisString("hello"));

Enter fullscreen mode Exit fullscreen mode
Output: olleh
Enter fullscreen mode Exit fullscreen mode

Note: how the for loop here works
(let i = stringLength - 1): we define a variable i with a starting value of stringLength - 1 (i.e 5 - 1 = 4) where index 4 represents the last index position o of the sample string (Hello).
(i >= 0): We tell it that i will always be greater than or equal to 0 (when it is less than 0, it should stop looping)
(i--): will continually decrement the loop at each iteration
Inside the for loop:
(reversedString += inputString[i];): At each iteration (as the condition above remains valid) store the value of the index position in the reversedString variable.

Declarative approach

Pseudocode:
This pattern will only take a single line of code.
Use step 1,2, 6 & 7 above

  1. use split method with a quotation marks split('') to divide the strings by character i.e. 'h','e','l','l','o'
  2. use reverse method with no quotation marks reverse()to iterate through the indexes of the splitted string thus reversing its position. i.e. 'o','l','l','e','h'
  3. use join method with quotation marks join('') to concatenate the reversed string back with-out spaces i.e. olleh

Solution:

function reverseThisString(inputString) {
    return inputString.split('').reverse().join('');
}

console.log(reverseThisString("olleh"));
console.log(reverseThisString("fantastic"));
Enter fullscreen mode Exit fullscreen mode
Output: hello
Output: citsatnaf
Enter fullscreen mode Exit fullscreen mode

Top comments (0)