Sitemap
Charles

Charles

3 ways to reverse a string (JavaScript)

3 min readMay 5, 2025

--

Press enter or click to view image in full size
Photo by Juanjo Jaramillo on Unsplash

If you are new to the world of algorithms, working on reversing a string is a good place to start. Data structures and algorithms can be daunting but with a lot of practice you will see the patterns.

As with every form of algorithmic question, there is usually more than one way to solve it. While some answers may be optimal, others can be chaotic.

Overview of Problem

We have a string:

 “Hello”

The expectation is to write an algorithm that will return:

“olleH”

This article will explore 3 ways of reversing a string. These are:
1. Split, reverse, join method.

2. Stack method

3. Simple for-loop method.

1 .Split, Reverse, Join Method

The whole idea is to:

  • use the string method “split” to convert the string into an array of strings
  • We then reverse the array
  • We then join the array.

This is what it would look like:

function reverseString(str=""){
const splitStr= str.split("");//splits string to an array
const reverseArray = splitStr.reverse() // reverse the array
const result = reverseArray.join(""); // rejoin the strings
return result;
}


console.log(reverseString("Hello"));
console.log(reverseString("canoe"));

You can even take it further and make it one line:

function reverseString(str=""){
const result = str.split("").reverse().join("");

return result;
}


console.log(reverseString("Hello"));
console.log(reverseString("canoe"));

Time Complexity : O(n).

Get Charles’s stories in your inbox

Join Medium for free to get updates from this writer.

Space Complexity : O(n)

2 .Stack method

The idea here is to

  • Create an empty stack and empty string.
  • Loop through the entire string
  • In each interaction take each letter of the string and place it into the stack
  • Loop through the stack and add each value into the empty string. Since stacks are L.I.F.O (Last In First Out) , it means we would be picking from the last to the first.

The explanation above will look like this:

function reverseString(str=""){
const stack = [];
let result = "";


for(let i = 0; i < str.length; i++){
stack.push(str[i])
}


for(let i = str.length -1; i >= 0; i--){
result += stack[i];
}


return result;
}


console.log(reverseString("Hello"));
console.log(reverseString("canoe"));

Time Complexity : O(n2). This is because “result += stack[i]” takes “n” operations in the worst case. Since it’s in a loop it brings us to “n2”

Space Complexity : O(n)

We can also tweak it like this:

function reverseString(str=""){
let result = [];




for(let i = str.length -1; i >= 0; i--){
result.push(str[i]);
}




return result.join("");
}




console.log(reverseString("Hello"));
console.log(reverseString("canoe"));

Time Complexity : O(n).

Space Complexity : O(n)

3. Simple for-loop method

The idea here is to

  • Create an empty string.
  • Loop from the end of the loop to the front. In each interaction add the value to the empty string.

It will look like this:

function reverseString(str=""){
let result = "";

// loop from the end of the string
for(let i = str.length -1; i >= 0; i--){
result += str[i];
}


return result;
}


console.log(reverseString("Hello"));
console.log(reverseString("canoe"));

Time Complexity : O(n2). This is because “result += stack[i]” takes “n” operations in the worst case. Since it’s in a loop it brings us to “n2”

Space Complexity : O(n)

This brings us to the end of this write-up. We successfully explored different ways to reversing a string.

I hope you learnt something new today. You will be surprised that people encounter this during an interview.

If you liked this please leave a clap and share. Gracias.

--

--