JavaScript how to make the first letter title

Learn how to capitalize the first letter of a string in Javascript with an example.

Making the First Letter Title with JavaScript

It's possible to capitalize the first letter of a string in JavaScript. This can be useful when you want to create titles or headings. To do this, you can use the substring() and toUpperCase() methods. Here is an example of how you can use these methods to capitalize the first letter of a string:


let str = "this is a title";

let firstLetter = str.substring(0, 1);
let restOfWord = str.substring(1);

let title = firstLetter.toUpperCase() + restOfWord;

console.log(title); // This is a title

In this example, we have a string called str. We use the substring() method to get the first letter of the string and then we use the toUpperCase() method to capitalize it. We then combine the capitalized letter with the rest of the word using the + operator and store the result in a new variable called title. When we log out the value of title, we can see that the first letter of the string has been capitalized.

This is just one example of how you can capitalize the first letter of a string in JavaScript. You can also use the charAt() and replace() methods to achieve the same result.

Answers (0)