Reverse a String in JavaScript
Reverse a string in JavaScript with split, reverse and join, plus loops, reduce, recursion and spread. Includes the emoji trap, with explanation and output.
1// Method 1: Using split, reverse, join2let str = "Hello";3let reversed = str.split('').reverse().join('');4console.log("Original:", str);5console.log("Reversed:", reversed);67// Method 2: Using for loop8function reverseString(str) {9 let reversed = '';10 for (let i = str.length - 1; i >= 0; i--) {11 reversed += str[i];12 }13 return reversed;14}1516console.log("\nUsing loop:", reverseString("World"));1718// Method 3: Using for...of loop19function reverseStringForOf(str) {20 let reversed = '';21 for (let char of str) {22 reversed = char + reversed;23 }24 return reversed;25}2627console.log("Using for...of:", reverseStringForOf("JavaScript"));2829// Method 4: Using reduce30function reverseStringReduce(str) {31 return str.split('').reduce((acc, char) => char + acc, '');32}3334console.log("Using reduce:", reverseStringReduce("Programming"));3536// Method 5: Using recursion37function reverseStringRecursive(str) {38 if (str.length <= 1) return str;39 return reverseStringRecursive(str.slice(1)) + str[0];40}4142console.log("\nUsing recursion:", reverseStringRecursive("Hello"));4344// Method 6: Using spread operator45let reversed2 = [...str].reverse().join('');46console.log("Using spread:", reversed2);
Sample Output
Original: Hello Reversed: olleH Using loop: dlroW Using for...of: tpircSavaJ Using reduce: gnimmargorP Using recursion: olleH Using spread: olleH
In this one
- Why strings are immutable
- How split, reverse and join work together
- Four other ways to reverse text
- Why spread handles emoji better than split
How it works
- 1
"Hello" is split, reversed and joined into "olleH".
- 2
The backward loop turns "World" into "dlroW".
- 3
for...of builds "tpircSavaJ".
- 4
reduce builds "gnimmargorP".
- 5
Recursion and spread both give "olleH".
Edge cases
Emoji with split
'hi๐'.split('').reverse().join('') shows broken symbols.
split cuts the emoji into halves. Use [...str] instead.
Very long strings with recursion
A very long string can throw a stack error.
Each call adds to the call stack. Use a loop for long text.
What's going on
This program turns "Hello" into "olleH". Reversing a string is a common interview warm-up and a building block for palindrome checks.
Strings in JavaScript are immutable. That means you cannot change a string in place. Every method here builds a new string.
Method 1 is the one most people remember: str.split('').reverse().join(''). split('') turns the string into an array of characters. reverse() flips the array. join('') glues it back into a string.
Method 2 uses a for loop that starts at the last index, str.length - 1, and counts down to 0, adding each character to a new string.
Method 3 uses for...of. It takes each character from the front and puts it before what you have so far. So "Java" builds J, aJ, vaJ, avaJ.
Method 4 does the same with reduce, starting from an empty string.
Method 5 uses recursion. reverseStringRecursive("Hello") returns reverseStringRecursive("ello") + "H". It keeps going until the string has 1 or 0 characters. Very long strings can run out of stack space.
Method 6 spreads the string: [...str].reverse().join(''). This handles most emoji better. split('') breaks an emoji like ๐ into two broken halves, because it uses two UTF-16 code units. Spread and for...of keep it whole. Complex emoji like family groups or flags can still break.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What does "abc".split("") return?
Pick an answer to check it.
Question 2
Which handles most emoji correctly?
Pick an answer to check it.
Questions people ask
Is there a built-in string reverse?
No. Strings have no reverse method, so you go through an array.
Which method is fastest?
The plain loop or split-reverse-join. Both are fine for normal text.
Does reversing change the original variable?
No. Strings cannot be changed. You get a new string.