CyrefJS
CCyrefJS
فا
Back to documentation
String/Since 1.38.0

reverse

Reverses a string by converting it into Unicode code points, reversing their order, and joining them back together.

reverse(value):string

Behavior

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

valuestringrequired

The string to reverse.

Returns

string

The input string with its Unicode code points in reverse order.

Examples

01

Basic usage

Reverse the characters of a string.

TypeScript
import { reverse } from "@cyref/js";

reverse("hello");
// "olleh"
02

Sentence

The entire string is reversed, including spaces.

TypeScript
reverse("hello world");
// "dlrow olleh"
03

Palindrome

A palindrome produces the same string when reversed.

TypeScript
reverse("level");
// "level"
04

Unicode characters

Array.from preserves Unicode code points such as common emoji as individual elements.

TypeScript
reverse("hello 😀");
// "😀 olleh"
05

Empty string

An empty string remains empty.

TypeScript
reverse("");
// ""
06

Single character

A single character remains unchanged.

TypeScript
reverse("A");
// "A"
Built with@cyref/js·Official documentation