CyrefJS
CCyrefJS
فا
Back to documentation
String/Since 1.38.0

capitalize

Converts the first character of a string to uppercase while leaving the rest of the string unchanged.

capitalize(value):string

Behavior

If the input string is empty, capitalize returns it unchanged. Otherwise, it converts the first character to uppercase and appends the rest of the original string unchanged.

Preserves the rest

capitalize does not lowercase or otherwise normalize the remaining characters. Only the first character is transformed.

Empty strings

An empty string is returned directly without modification.

Parameters

valuestringrequired

The string whose first character should be capitalized.

Returns

string

The string with its first character converted to uppercase.

Examples

01

Basic usage

Capitalize the first character of a lowercase string.

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

capitalize("hello");
// "Hello"
02

Mixed case

Only the first character is changed. The rest remains unchanged.

TypeScript
capitalize("hello WORLD");
// "Hello WORLD"
03

Already capitalized

A string that already starts with an uppercase character remains unchanged.

TypeScript
capitalize("Hello");
// "Hello"
04

Empty string

An empty string is returned unchanged.

TypeScript
capitalize("");
// ""
05

Rest of the string

Characters after the first one are preserved exactly as they are.

TypeScript
capitalize("hELLO wORLD");
// "HELLO wORLD"
06

Single character

A single character is converted to uppercase.

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