One data grid for JavaScript, React, Vue & Angular. Sorting, filtering, virtualization, pinning, and CSV export included. Early access $199 $349 → [Get it now]

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 React components for formatting user names, form labels, and content headings throughout our component ecosystem. For empty strings, this method returns an empty string safely since charAt(0) returns '' rather than undefined. For more complex capitalization like title case, consider libraries like Lodash. If you need to manipulate other parts of a string, learn how to replace all occurrences in a string in JavaScript for broader text transformations.


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

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.
JavaScript printf equivalent
JavaScript printf equivalent

How to Open All Links in New Tab Using JavaScript
How to Open All Links in New Tab Using JavaScript

How to Remove Underline from Link in CSS
How to Remove Underline from Link in CSS

Understanding and Resolving the “React Must Be in Scope When Using JSX
Understanding and Resolving the “React Must Be in Scope When Using JSX

Answers by CoreUI Core Team