Simple JavaScript function to capitalize first letter of a word

Hi! Previously I was searching for a way to capitalize the first letter of a word in JavaScript. I thought there is built in function/method in JavaScript to do that, but I didn’t find it.

So, here is a simple function that I’ve made.

function capFirstLetter(txt){
	return txt.charAt(0).toUpperCase() + txt.slice(1)
}

To use this function, just call the function with any word as its parameter that you want to capitalize its first letter. For example:

capFirstLetter("hello")

Then it will return “Hello” instead of “hello”.

Leave a Reply

Your email address will not be published. Required fields are marked *