Skip to main content

upperFirst()

upperFirst(str): string

Converts the first character of a string to uppercase and the rest to lowercase.

๐Ÿ’Ž Why is this a Hidden Gem?

Capitalize just the first character โ€” ideal for display names and UI labels.

DEPRECATED

Use capitalize from Arkhe directly instead.


Parametersโ€‹

str: stringโ€‹

The string to convert.


Returns: stringโ€‹

The string with its first character converted to uppercase and the rest lowercased.


See Alsoโ€‹

String.toUpperCase() - MDN


Sinceโ€‹

2.0.0


Also known asโ€‹

upperFirst (Lodash, es-toolkit) ยท โŒ (Remeda, Radashi, Ramda, Effect, Modern Dash, Antfu)


Exampleโ€‹

// โŒ Deprecated approach
upperFirst('hello'); // => 'Hello'
upperFirst('HELLO'); // => 'Hello'

// โœ… Recommended approach
import { capitalize } from 'pithos/arkhe/string/capitalize';
capitalize('hello'); // => 'Hello'

How it works?โ€‹

Converts the first character to uppercase. Deprecated: Use str[0].toUpperCase() + str.slice(1) directly.

Native Equivalentโ€‹

// โŒ upperFirst('hello')
// โœ… 'hello'[0].toUpperCase() + 'hello'.slice(1)

Use Casesโ€‹

Capitalize first letter ๐Ÿ“Œโ€‹

Uppercase first character only.

const result = upperFirst('hello'); // 'Hello'

Format user-generated sentencesโ€‹

Ensure the first letter of user-submitted text is capitalized for consistent display.

const formatComment = (text: string) => upperFirst(text.trim());

formatComment('great article, thanks!');
// => 'Great article, thanks!'

Build component names from stringsโ€‹

Capitalize the first letter when constructing dynamic component or class names.

const getComponentName = (type: string) => `Icon${upperFirst(type)}`;

getComponentName('arrow'); // 'IconArrow'
getComponentName('check'); // 'IconCheck'