How to capitalize the first letter of a string in JavaScript

Capitalizing the first letter of strings is essential for proper name formatting, title case conversion, sentence formatting, and creating polished user interfaces in JavaScript applications. With over 25 years of experience in software development and as the creator of CoreUI, I’ve implemented first letter capitalization in components like form inputs, user profiles, and content displays where proper text formatting enhances professionalism and readability. From my extensive expertise, the most reliable and widely supported solution is combining charAt(0).toUpperCase() to capitalize the first character with slice(1) to preserve the rest of the string. This approach handles edge cases gracefully and works consistently across all JavaScript environments.

Use charAt(0).toUpperCase() plus slice(1) to capitalize the first letter of a string.

const text = 'hello world'
const capitalized = text.charAt(0).toUpperCase() + text.slice(1)
// Result: 'Hello world'

This method combines two operations: charAt(0).toUpperCase() gets the first character and converts it to uppercase, while slice(1) extracts the remainder of the string starting from index 1. In this example, 'hello world'.charAt(0) gets ‘h’, toUpperCase() makes it ‘H’, and slice(1) gets ’ello world’, resulting in ‘Hello world’. The charAt() method safely handles empty strings by returning an empty string, preventing errors that could occur with bracket notation.

Best Practice Note:

This is the same approach we use in CoreUI components for formatting user names, form labels, and content headings throughout our component ecosystem. For empty strings, this method returns an empty string safely. For more complex capitalization like title case, consider libraries like Lodash. Modern alternative: text[0]?.toUpperCase() + text.slice(1) uses optional chaining for even safer handling of edge cases.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author