Reverse a String

Program to reverse a string

BeginnerTopic: String Programs
Back

JavaScript Reverse a String Program

This program helps you to learn the fundamental structure and syntax of JavaScript programming.

Try This Code
// Method 1: Using split, reverse, join
let str = "Hello";
let reversed = str.split('').reverse().join('');
console.log("Original:", str);
console.log("Reversed:", reversed);

// Method 2: Using for loop
function reverseString(str) {
    let reversed = '';
    for (let i = str.length - 1; i >= 0; i--) {
        reversed += str[i];
    }
    return reversed;
}

console.log("\nUsing loop:", reverseString("World"));

// Method 3: Using for...of loop
function reverseStringForOf(str) {
    let reversed = '';
    for (let char of str) {
        reversed = char + reversed;
    }
    return reversed;
}

console.log("Using for...of:", reverseStringForOf("JavaScript"));

// Method 4: Using reduce
function reverseStringReduce(str) {
    return str.split('').reduce((acc, char) => char + acc, '');
}

console.log("Using reduce:", reverseStringReduce("Programming"));

// Method 5: Using recursion
function reverseStringRecursive(str) {
    if (str.length <= 1) return str;
    return reverseStringRecursive(str.slice(1)) + str[0];
}

console.log("\nUsing recursion:", reverseStringRecursive("Hello"));

// Method 6: Using spread operator
let reversed2 = [...str].reverse().join('');
console.log("Using spread:", reversed2);
Output
Original: Hello
Reversed: olleH

Using loop: dlroW

Using for...of: tpircSavaJ

Using reduce: gnimmargorP

Using recursion: olleH

Using spread: olleH

Understanding Reverse a String

This program demonstrates different methods to reverse a string.

Method 1: Split, Reverse, Join

Most common approach:

str.split('').reverse().join('');

Steps:

1.split(''): String → Array
2.reverse(): Reverse array
3.join(''): Array → String

Method 2: For Loop

Iterate backwards:

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

Method 3: For...Of Loop

Prepend each character:

for (let char of str) {
    reversed = char + reversed;
}

Method 4: Reduce

Functional approach:

str.split('').reduce((acc, char) => char + acc, '');

How it works:

Starts with empty string
Prepends each character
Result is reversed

Method 5: Recursion

Recursive approach:

if (str.length <= 1) return str;
return reverseStringRecursive(str.slice(1)) + str[0];

How it works:

Base case: length <= 1
Recursive: reverse rest + first char

Method 6: Spread Operator

ES6 syntax:

[...str].reverse().join('');

Spread vs Split:

Spread: ES6, works with iterables
Split: Traditional, string-specific

Time Complexity:

All methods: O(n)

When to Use:

-

Split/reverse/join

: Simplest, most common

-

Loop

: Learning, custom logic

-

Reduce

: Functional style

-

Recursion

: Learning recursion

Let us now understand every line and the components of the above program.

Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.

Practical Learning Notes for Reverse a String

This JavaScript program is part of the "String Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.

Table of Contents