How to Uppercase the First Letter of a String in JavaScript?

To uppercase only the first letter in a string in JavaScript, you can use the following code:

var str = "Hello World!";
var newStr = str.charAt(0).toUpperCase() + str.slice(1);

The result will be:

"Hello world!"

To reuse this code multiple times, you can use the following function:
function uppercaseFirstLetter(str) {
return str.charAt(0).toUpperCase() + str.slice(1);

You can also capitalize the entire string or lowercase the whole string.


Please enter your comment!
Please enter your name here