How to Uppercase the first letter of a string in JavaScript

Snippets Of JavaScript

The following code snippet uses the JavaScript function charAt(0) to get the first character of the string and then applies the toUpperCase() function to capitalize the character.

We can then add the remainder of the string to the first character using the slice(1) function.

const myString = 'mountain'
let myStringFirstCharCapitalized = myString.charAt(0).toUpperCase()
myStringFirstCharCapitalized += myString.slice(1)

Another approach is to use CSS, if you just want to capitalize for presentation on a web page. You can do this with text-transform as follows:

.capitalize {
  text-transform: capitalize;
}