How to Capitalize the First Letter of a String in JavaScript

Published: Wednesday, May 11, 2022
Updated: Thursday, May 12, 2022

Greetings, friends! Here is a simple approach for capitalizing the first letter of a string using JavaScript.

Copied! ⭐️
const str = 'nice!';

console.log(str.charAt(0).toUpperCase() + str.slice(1));
// OUTPUT: Nice!

In the code above, we can grab the first letter of a string by using the String.charAt method. We can then use String.toUpperCase method to uppercase the first letter.

The String.slice method retrieves part of a string, starting at a specified index. By passing in the value, 1, to this method, we are extracting the entire string except the first character.

Finally, we can use the string concatenation operator to create the capitalized string!