home ~ projects ~ socials

Capitalize the First Letter of a Word in JavaScript

This is the way I'm capitalizing single words in JavaScript:

JavaScript

function capitalizeWord(word) {
  return word.charAt(0).toUpperCase() + word.slice(1);
}

let word = capitalizeWord("alfa");

document.querySelector(".word-output").innerHTML = word;

HTML

<div class="word-output"></div>

Output

-- end of line --

Endnotes

This is about as basic as it get.

I don't have to worry about words starting with something other than letters for my use case (i.e. I don't have to try to file the first letter after an underscore or something). Covering that is possible. It's just more complicated than I need.