docs/reference/compat/string/capitalize.md
::: warning Use capitalize from es-toolkit
This capitalize function operates slower due to handling non-string input values.
Instead, use the faster and more modern capitalize from es-toolkit.
:::
Converts the first character of a string to uppercase and the remaining characters to lowercase.
const result = capitalize(str);
capitalize(str)Converts the first character of a string to uppercase and the remaining characters to lowercase. This is useful for making the first impression of a word better or formatting it as a title.
import { capitalize } from 'es-toolkit/compat';
capitalize('fred'); // 'Fred'
capitalize('FRED'); // 'Fred'
capitalize('fRED'); // 'Fred'
Empty strings and non-string values can also be handled.
import { capitalize } from 'es-toolkit/compat';
capitalize(''); // ''
capitalize(123); // '123'
capitalize(null); // ''
capitalize(undefined); // ''
str (string, optional): The string to capitalize.(string): Returns the string with the first character capitalized and the remaining characters in lowercase.