W3docs

String Functions

String Reverse

Reverse the order of characters in any string. Unicode-aware — emoji and accented characters reverse cleanly.

About String Reverse

String Reverse flips the order of characters in a block of text. Paste text into the Source field and the Result field shows, as you type, the input read backwards — words, spaces, punctuation, and line breaks are all reversed together, so hello world becomes dlrow olleh, not two separately-reversed words. The transform runs entirely in your browser; nothing you paste is sent to a server.

Under the hood it splits the text into grapheme clusters with Intl.Segmenter — the units a reader sees as one character — and reverses those. That keeps emoji sequences like 👨‍👩‍👧 (several code points joined by zero-width joiners), flags, and letters with combining accents such as e + U+0301 intact instead of scrambling their parts. Where Intl.Segmenter is unavailable it falls back to reversing by Unicode code point. The common one-liner input.split('').reverse().join('') reverses UTF-16 code units and corrupts any of these; PHP's strrev() reverses raw bytes and breaks every multi-byte UTF-8 character.

Reach for this for reverse-a-string exercise fixtures, to sanity-check your own reversal implementation against a known-good reference, to generate quick reversed placeholder strings, or as the first step of a palindrome check.

Examples

Reversing a sentencetext
Input:  Was it a car or a cat I saw?
Output: ?was I tac a ro rac a ti saW

This is the tool's own placeholder text — try it in the widget above.

Why grapheme clusters beat split('') and Array.fromjs
const input = 'a👨‍👩‍👧b';

input.split('').reverse().join('');
// garbage — surrogate pairs and joiners torn apart

Array.from(input).reverse().join('');
// the family emoji is split into three people in the wrong order

const seg = new Intl.Segmenter(undefined, { granularity: 'grapheme' });
[...seg.segment(input)].map((s) => s.segment).reverse().join('');
// "b👨‍👩‍👧a" — correct; this is what String Reverse does

split('') iterates UTF-16 code units and Array.from iterates code points; only Intl.Segmenter iterates what a reader sees as characters.

Palindrome check built on reversaljs
function isPalindrome(s) {
  const clean = s.toLowerCase().replace(/[^a-z0-9]/g, '');
  return clean === [...clean].reverse().join('');
}

isPalindrome('racecar');                    // true
isPalindrome('A man a plan a canal Panama'); // true
isPalindrome('hello');                       // false

After stripping everything but ASCII letters and digits, code-point reversal is safe.

Frequently asked questions

How do I reverse a string in JavaScript?

For plain ASCII, str.split('').reverse().join('') is enough. To keep emoji sequences and accented letters intact, split into grapheme clusters with Intl.Segmenter and reverse those.

Why does str.split('').reverse().join('') break on some strings?

split('') divides a string into UTF-16 code units, not characters. Emoji outside the Basic Multilingual Plane are stored as two code units, and sequences like 👨‍👩‍👧 or e + a combining accent are several code points, so reversing them separates parts that belong together.

Can I use string reversal to check for a palindrome?

Yes — normalize the string first (lowercase it, strip spaces and punctuation), then compare it to its reversed form. If they match, it's a palindrome.

Does this tool send my text to a server?

No. The reversal runs client-side in your browser — the text in the Source field is never sent anywhere.

Does reversing a string change its length?

No visible characters are added or removed, and reversing the result a second time returns the original text. A string's .length stays the same too, since the same code units are only reordered.