Lodash upperFirst vs capitalize: Key Differences

When working with string manipulation in the Lodash JavaScript library, both _.upperFirst and _.capitalize convert the initial character of a string to uppercase, but they handle the remaining characters fundamentally differently. While _.upperFirst alters only the very first character and leaves the rest of the string untouched, _.capitalize enforces standard sentence casing by converting the first character to uppercase and forcefully converting all subsequent characters to lowercase.

How _.upperFirst Works

The _.upperFirst method converts only the first character of a string to uppercase. Any casing present throughout the rest of the string is strictly preserved.

const _ = require('lodash');

_.upperFirst('fred');     // => 'Fred'
_.upperFirst('FRED');     // => 'FRED'
_.upperFirst('fooBar');   // => 'FooBar'

Because the remaining characters are unaffected, _.upperFirst is ideal when transforming camelCase identifiers into PascalCase (such as converting component or class names), or when preserving acronyms inside strings.

How _.capitalize Works

The _.capitalize method converts the first character to uppercase, but it also transforms every subsequent character to lowercase, regardless of its original case.

const _ = require('lodash');

_.capitalize('fred');     // => 'Fred'
_.capitalize('FRED');     // => 'Fred'
_.capitalize('fooBar');   // => 'Foobar'

This method is designed to normalize user input or enforce standard sentence casing where only the initial letter should be capitalized.

Side-by-Side Comparison

Input String _.upperFirst(input) _.capitalize(input)
'hello' 'Hello' 'Hello'
'HELLO' 'HELLO' 'Hello'
'camelCase' 'CamelCase' 'Camelcase'
'iOS' 'IOS' 'Ios'

Summary of When to Use Each