0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Reverse a String

You can reverse a string in JavaScript by using split('') to turn it into an array, then reverse() the array, and finally join('') it back into a string like this: const reversed = str.split('').reverse().join('');
📋

Examples

Inputhello
Outputolleh
InputJavaScript
OutputtpircSavaJ
Input
Output
🧠

How to Think About It

To reverse a string, think of it like flipping the order of letters. First, break the string into single letters so you can move them around. Then, flip the order of these letters. Finally, put the letters back together to form the reversed string.
📐

Algorithm

1
Get the input string.
2
Split the string into an array of characters.
3
Reverse the order of the array elements.
4
Join the reversed array back into a string.
5
Return or print the reversed string.
💻

Code

javascript
const reverseString = str => str.split('').reverse().join('');

const input = 'hello';
const output = reverseString(input);
console.log(output);
Output
olleh
🔍

Dry Run

Let's trace the string 'hello' through the code

1

Input string

str = 'hello'

2

Split into array

['h', 'e', 'l', 'l', 'o']

3

Reverse array

['o', 'l', 'l', 'e', 'h']

4

Join array to string

'olleh'

StepArray State
After split['h', 'e', 'l', 'l', 'o']
After reverse['o', 'l', 'l', 'e', 'h']
💡

Why This Works

Step 1: Splitting the string

Using split('') breaks the string into an array of single characters so we can reorder them.

Step 2: Reversing the array

The reverse() method flips the order of elements in the array, putting the last character first.

Step 3: Joining back to string

Finally, join('') combines the reversed array back into a single string without spaces.

🔄

Alternative Approaches

Using a for loop
javascript
function reverseStringLoop(str) {
  let reversed = '';
  for (let i = str.length - 1; i >= 0; i--) {
    reversed += str[i];
  }
  return reversed;
}

console.log(reverseStringLoop('hello'));
This method manually builds the reversed string and is easy to understand but less concise.
Using recursion
javascript
function reverseStringRec(str) {
  if (str === '') return '';
  return reverseStringRec(str.substr(1)) + str[0];
}

console.log(reverseStringRec('hello'));
This uses a recursive approach but can be less efficient and harder for beginners.

Complexity: O(n) time, O(n) space

Time Complexity

The code processes each character once when splitting, reversing, and joining, so it runs in linear time relative to string length.

Space Complexity

Extra space is used to store the array of characters and the reversed array, so space grows linearly with input size.

Which Approach is Fastest?

The built-in split-reverse-join method is usually fastest and most readable; loops are similar in speed but more verbose; recursion is slower and uses more stack space.

ApproachTimeSpaceBest For
split-reverse-joinO(n)O(n)Quick and readable code
for loopO(n)O(n)Manual control, beginner-friendly
recursionO(n)O(n)Learning recursion, less efficient
💡
Use split(''), reverse(), join('') for a quick and readable string reversal in JavaScript.
⚠️
Beginners often forget to join the reversed array back into a string, leaving an array instead of a string.