utilcn logoutilcn
String Utilities

reverseString

Reverses the characters in a string

Usage

import { reverseString } from '@/lib/reverse-string';

const result = reverseString("TypeScript");
console.log(result); // "tpircSpeyT"

Installation

pnpm dlx shadcn@latest add https://utilcn.dev/r/reverse-string.json
bunx --bun shadcn@latest add https://utilcn.dev/r/reverse-string.json
npx shadcn@latest add https://utilcn.dev/r/reverse-string.json
yarn shadcn@latest add https://utilcn.dev/r/reverse-string.json

Parameters

ParameterTypeDescription
strstringThe input string

Returns

string - The reversed string.

Examples

reverseString("TypeScript"); // "tpircSpeyT"
reverseString("Hello World"); // "dlroW olleH"
reverseString("12345"); // "54321"
reverseString("a"); // "a"
reverseString(""); // ""

Implementation

export function reverseString(str: string): string {
  return str.split('').reverse().join('');
}

Performance Note

This implementation is simple and readable. For very large strings, you might consider other approaches, but for typical use cases, this method is efficient and clear.