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β
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'