home ~ socials ~ projects ~ rss

Capitalize the First Letter of a Word in JavaScript

May 2025

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

Alfa
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.

Share link:
https://www.alanwsmith.com/en/2x/q6/gl/q3/?capitalize-the-first-letter-of-a-word-in-javascript