Why the usual one-liner destroys emoji
text.split('').reverse().join('') is the first answer everywhere, and it is wrong for anything but plain ASCII.
It splits characters in half. A JavaScript string is UTF-16 code units, and any character outside the Basic Multilingual Plane, which is nearly every emoji, is a pair. split('') hands back the halves separately, so reversing leaves unpaired surrogates that render as replacement boxes.
Fixing that is not enough. Reversing by code point with [...text].reverse().join('') keeps single emoji intact and breaks anything built from more than one. The family emoji becomes three separate people. An e with a combining acute becomes an accent floating in front of a bare e. A flag is a pair of regional indicators, and swapping them gives a different country or nothing.
The unit you want is the grapheme cluster (Unicode Annex #29): the run of code points a reader sees as one character. This tool finds those with Intl.Segmenter, so emoji, flags, skin-tone modifiers, combining accents and Devanagari and Thai clusters all survive.
Common problems
- Reversed Arabic or Hebrew looks scrambled. It is. Those scripts are written right to left and the browser already handles that on display. Reversing the stored order fights the display algorithm rather than cooperating with it.
- Word mode keeps punctuation attached. "Hello, big world" becomes "world big Hello,". Splitting on Unicode word boundaries would peel the comma off and scatter punctuation through the result. Spacing stays put, so indentation and column gaps survive.
- Windows line endings come back as Unix ones. Carriage returns are normalised to line feeds first, since a stray CR mid-line would be invisible corruption.
- The trailing newline did not move. Treating it as another character would give every reversed file a blank first line.
Frequently asked questions
Does reversing twice give me my text back?
In Characters and Lines mode yes, except that CRLF comes back as LF. Words mode round-trips on single-spaced text only, since the spacing stays put while the words move.
Can I use this to check for a palindrome?
For a single word, yes. For a phrase it will disagree: "A man, a plan, a canal: Panama" is only a palindrome once spaces, punctuation and capitals are stripped.
How much text can I reverse at once?
2 MB. Finding grapheme boundaries is most of the cost, so half a megabyte reverses in well under a second. Above 20,000 characters the output updates a moment after you stop typing.
Why does reversing break my emoji in other tools?
Because they reverse code units. A flag emoji is two regional indicators and a family is several people joined by zero-width joiners, so splitting on characters and reversing scrambles them into different emoji or into nothing. This page reverses graphemes with Intl.Segmenter, which keeps each one whole.