How to make the first letter of a String Uppercase in JavaScript

Srijan } Author Pic
SrijanJan 20, 2025 - JavaScript
How to make the first letter of a String Uppercase in JavaScript - Reacted Node

Photo: Mart Production/Pexels

Introduction

In this article, we will learn how to make the first letter of a string uppercase (also known as capitalizing the string). Capitalizing the string helps format user inputs, display names or titles, and enhance the overall presentation of web application text.

Table of Contents

charAt(), toUpperCase() and slice() to the rescue

Let's write a simple solution to capitalize the string using the following JavaScript string methods:

charAt()

charAt() returns the character at the specified index in a string. For example, str.charAt(0) returns the first character of the string str.

1const str = "hello world!";
2console.log(str.charAt(0)); // Outputs: h

toUpperCase()

toUpperCase() converts the entire string to uppercase letters.

1const str = "hello world!";
2console.log(str.toUpperCase()); // Outputs: HELLO WORLD!

slice()

slice() extracts a part of a string and returns it as a new string. For example, str.slice(1) returns the string from the second character to the end of the string.

1const str = "hello world!";
2console.log(str.slice(1)); // Outputs: ello world!

Step-by-Step Implementation

Now, let's complete our solution for capitalizing the string using the above methods:

  1. First, we will extract the first character using the charAt() method.
  2. Then, we will convert the extracted character to uppercase using the toUpperCase() method.
  3. At last, we will get the remaining string, leaving the first character using the slice() method and concatenating it with the first character that we extracted and converted to uppercase in the first two steps.
1function capitalizeFirstLetter(str) {
2 if (str.length === 0) return str; // Check if the string is empty
3
4 return str.charAt(0).toUpperCase() + str.slice(1);
5}
6
7// Example Usage
8let myString = "hello world";
9let capitalizedString = capitalizeFirstLetter(myString);
10console.log(capitalizedString); // Output: "Hello world"