Make first letter of a string uppercase in JavaScript?

To make first letter of a string uppercase, use toUpperCase() in JavaScript. With that, we will use charAt(0) since we need to only capitalize the 1st letter.

Syntax

string.charAt(0).toUpperCase() + string.slice(1)

Example

function replaceWithTheCapitalLetter(values){
    return values.charAt(0).toUpperCase() + values.slice(1);
}
var word = "javascript"
console.log(replaceWithTheCapitalLetter(word));
Javascript

Multiple Examples

function capitalizeFirst(str) {
    return str.charAt(0).toUpperCase() + str.slice(1);
}

console.log(capitalizeFirst("hello world"));
console.log(capitalizeFirst("programming"));
console.log(capitalizeFirst("a"));
console.log(capitalizeFirst(""));
Hello world
Programming
A

How It Works

The method breaks down into three parts:

  • charAt(0) - Gets the first character at index 0
  • toUpperCase() - Converts that character to uppercase
  • slice(1) - Gets the rest of the string from index 1 onwards

Edge Cases

function safeCapitalize(str) {
    if (!str || str.length === 0) return str;
    return str.charAt(0).toUpperCase() + str.slice(1);
}

console.log(safeCapitalize(""));        // Empty string
console.log(safeCapitalize("a"));       // Single character
console.log(safeCapitalize("HELLO"));   // Already uppercase

A
HELLO

Conclusion

Use charAt(0).toUpperCase() + string.slice(1) to capitalize the first letter. Handle empty strings to avoid errors in production code.

Updated on: 2026-03-15T23:18:59+05:30

480 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements