1.38.0reverse
Reverses a string by converting it into Unicode code points, reversing their order, and joining them back together.
reverse(value):stringBehavior
reverse converts the string into an array using Array.from, reverses the array, and joins the elements back into a string.
Unicode handling
Because Array.from is used instead of splitting the string by UTF-16 code units, Unicode code points represented by surrogate pairs are kept together during reversal.
Whitespace and punctuation
All characters represented as elements by Array.from participate in the reversal, including spaces and punctuation.
Original string
The original string is not modified. A new string is produced from the reversed array.
Parameters
valuestringrequiredThe string to reverse.
Returns
stringThe input string with its Unicode code points in reverse order.
Examples
Basic usage
Reverse the characters of a string.
import { reverse } from "@cyref/js";
reverse("hello");
// "olleh"Sentence
The entire string is reversed, including spaces.
reverse("hello world");
// "dlrow olleh"Palindrome
A palindrome produces the same string when reversed.
reverse("level");
// "level"Unicode characters
Array.from preserves Unicode code points such as common emoji as individual elements.
reverse("hello 😀");
// "😀 olleh"Empty string
An empty string remains empty.
reverse("");
// ""Single character
A single character remains unchanged.
reverse("A");
// "A"